Abstract: User controls provided in ASP.NET can solve the problem that the code reuse in the ASP is more convenient for the error check in debugging. This article uses the user control implementation method and the implementation of a user control routine, further verifying the feasibility and effectiveness of using the user control to resolve code reuse.
[Keyword] code reuse, user control, @ register directive
ASP.NET provides better code separation scheme than traditional ASPs. In a conventional ASP, the code of the ASP file or transaction object component to be executed with Server. Execute is separated, and only the code can only be separated into several files and then use "include". This method will result in a copy of multiple "incrude" files in memory (a file is generated once every time you are referenced), thereby increasing the burden on the system, so it is not feasible. Using Server. Execute may be changed, but the additional query string parameters cannot be passed to the ASP file being executed. Using components may be a relatively better approach. In ASP.NET, in addition to using "include" and transaction object components, two new technologies: Code-BEHIND class and web user controls.
Use the Code-BEHIND class to effectively leave the client's HTML code and server-side event processing code. Divide these two-part code separated the tracker when facilitating debugging, as you don't have to switch between code and transaction logic code that implements user interfaces and transaction logic code. Moreover, if the user interface and transaction processing in the project are developed by different groups, the use of the Code-Behind class can make these two parts to implement independent debugging. Separate user interface code and transaction logic code space to make the code integration and maintenance of the code using the Code-BEHIND class. The user control will be described below to process the code separation problem from the perspective of the code reuse.
1. Introduction to User Controls
User control allows programmers to easily divide and reuse public UI functions across ASP.NET web applications. As with the web form page, programmers can use any text editor to create user controls, or use code hidden class to develop user controls. In addition, like the web form page, the user control can be compiled and stored in the server memory during the first request, thereby shortening the response time of the subsequent request. However, different from the web form page is that the user control cannot be independently requested, and the user control must be included in the web form page.
Compared with the server-side containing file (SSI), user controls are supported by accessing object models provided by ASP.NET to make programmers with greater flexibility. Programmers can program any properties declared in the control, not just the functions provided by other files, which is the same as any other ASP.NET server control.
Although a language is required when you create user controls, programmers can include multiple user controls in a web form page created in multiple languages. For example, you can create a user control with Visual Basic.net, import data from the XML file, and create another user control with C #, which contains an order form, then includes these two in the same web form page. Control.
In addition, the output of the control can be cached independent of the part other than the control in the web form page including the user control. This technology is called a fragment cache, appropriately uses this technology to improve the performance of the site. For example, if the user control contains an ASP.NET server control that proposes a database, but the rest of this page contains only text and simple code running on the server, the programmer can perform fragment cache for the user control to improve the application. performance.
2. Creating a User Control You can create a user control by using a text or HTML editor to declare. User control declaration syntax is very similar to the syntax used by creating a web form page; the main difference between the two is that the user control does not include ,
, and