User Controls enable programmers to easily divide and reuse the public UI functions across ASP. NET web applications. Similar to web forms pages, programmers can use any text editor to create user controls, or use code hiding to develop user controls. In addition, like web forms pages, user controls can be compiled and stored in the server memory during the first request, thus reducing the response time of subsequent requests. However, unlike web forms pages, user controls cannot be requested independently. user controls must be included in web forms pages before they can be used.
Compared with server-side file inclusion (SSI), user controls allow programmers to have more flexibility by accessing the object model supported by ASP. NET. Programmers can program any attribute declared in the control, not just the functions provided by other files, just like any other ASP. NET Server Control.
Although you need to select a language when creating a user control, a programmer can include multiple user controls in a web form page created in multiple languages. For example, you can use Visual Basic. net creates a user control, imports data from XML files, and then creates another user control using C #. The control contains an order form, the two controls are included in the same web form page.
In addition, the output of the control can be cached independently of other components in the web forms page containing the user control. This technology is called fragment caching. Using it appropriately can improve the performance of the site. For example, if the user control contains ASP.. NET Server Control, but the rest of the page only contains text and simple code running on the server, the programmer can execute fragment cache on the user control to improve the performance of the application.
2. Create a user control
You can create a user control using a text or HTML editor. The user control declaration syntax is similar to the syntax used to create a web form page; the main difference between the two is that user controls do not include <HTML>, <body>, and <form> elements around the content. These elements are included in the web forms page that contains the user control.
User Controls can be as simple as text files, or they can contain other ASP. NET Server controls. The following process briefly introduces a simple logon form that can be included on multiple pages of the application.
Expose attributes, event handlers, and any other code to be included in the user control function in the Code declaration block. You have two options when using the properties of the user control. First, you can define new attributes of user controls and operate on them. Second, you can operate the server control properties that constitute the user control. For example, you can use the password. Text syntax to perform operations on the text attributes of a Web server control whose ID is password.
[Note] Any ASP. all attributes and methods of the. NET Server Control will be upgraded to the public attributes and methods of the user control.
It is easy to include user controls on the web forms page.
On the web forms page that contains the user control, declare a @ register command, which includes:
Tagprefix attribute, which associates the prefix with the user control. This prefix is included in the start tag of the user control element.
Tagname attribute, which associates the name with the user control. This name will be included in the start tag of the user control element.
Src attribute, which defines the virtual path of the user control file to be included in the web form page.
For example, the following code registers the user control defined in the loginform. ascx file. The control is also specified with the tag prefix "Acme" and the tag name "login.
<% @ Register tagprefix = "Acme" tagname = "loginform" src = ". \ loginform. ascx" %>
For example, to declare the control imported in the previous step, use the following syntax.
<Acme: loginform id = "loginform" runat = "server"/>