Web Forms User Control
Introduction to User Controls
In addition to the built-in server controls provided by ASP.net, you can easily define your own controls by using the techniques you already have to make Web FORMS pages. In fact, with just a little change, almost any Web Forms page can be reused on other pages as a server control. (Note that the type of the user control is System.Web.UI.UserControl and it inherits directly from System.Web.UI.Control). When the Web Forms page is used as a server control, it is referred to as a user control. After a transition, the. ascx extension is typically used to indicate the user control. This ensures that the user control file cannot be executed as a separate Web Form page (you can see that there is a little, but important, difference between the user control and the Web Forms page). User controls are included in the Web Forms page using the Register directive:
<%@ Register tagprefix= "ACME" tagname= "message" src= "Pagelet1.ascx"%>
TagPrefix determines a unique namespace for the user control. (This way, multiple user controls with the same name can be distinguished from each other). The value of the TagName is the unique name of the user control (you can use any name). The SRC attribute is the virtual path of the user control-for example, "Mypagelet.ascx" or "/myapp/include/mypagelet.ascx". After registering the user control, you can place the user control label on the Web Forms page as if it were a normal server control (including the runat= "Server" attribute):
<acme:message runat= "Server"/>
The following example shows the introduction of a user control in a Web Forms page. The user control in this example is just a simple static file.
To extend the properties of a user control
When the Web Forms page is processed as a user control, the public fields and methods of the Web Forms page are upgraded to public properties (that is, label properties) and public methods (the label's method). The following example extends the user control in the previous example by adding two public string fields. Note that these two fields can be declared directly in the containing page, or can be controlled by a program.
In addition to upgrading public fields to control properties, you can also use property syntax. Using attribute syntax is better than executing code to set or get a property value. The following example demonstrates an address user control that binds the Text property of a TextBox control. The advantage of this is that the control "free" inherits the automatic state management of the TextBox control. Note that there are two address user controls in the Include Web Forms page that set the Caption property for billing address and Shipping address, respectively. The real power of user controls is this reuse.
Another useful place for user controls is the Login control, which collects user names and passwords.
In this example, the server validation control is added to the login user control
Encapsulate events in a user control
As with normal server controls, the user control participates in the lifecycle of the entire request execution. This means that the user control can manipulate its own events and encapsulate some of the page logic from the containing Web Forms page.
The following example shows a product list user control, which handles its own commit return internally. Note that the user control itself does not have a binding <form runat= "Server" > control. Because only one form control appears on the page (ASP.net does not allow nested server forms), it is left to include the Web Forms page to define him.
To create a user control with a program
Just as a normal server control can be created with a program, so does the user control. The LoadControl method of the page is used to load the user control, while the virtual path is passed to the source file of the user control.
Control C1 = LoadControl ("Pagelet7.ascx");
((PAGELET7CS) C1). Category = "Business";
PAGE.CONTROLS.ADD (C1);
The type of the user control is determined by the ClassName property of the Controls command. For example, a user control with a filename saved as "Pagelet7.ascx" is assigned the "Pagelet7cs" strong type:
<%@ control Classname= "Pagelet7cs"%>
Because the type returned by the LoadControl method is System.Web.UI.Control, it must throw the appropriate strong type in order to set the individual properties of the user control. Eventually, the user control is added to the ControlCollection collection of the base page.
Important for Web Forms pages that contain user controls, the strong type of the user control is available only if the Register command contains a user control (even if no user control label is actually declared).
Summary of this chapter
1. User controls allow developers to easily create custom controls by using the program design techniques used to create Web Forms pages.
2, as the difference with the normal page, use the. ascx file name extension to indicate the user control. This ensures that the user control cannot be executed like the standard Web Forms page.
3. Use the Register command to include the user control in the Web Forms page. The Register command specifies TagPrefix, TagName, and SRC location.
4. After registering the user control, you can use the user control (including the runat= "Server" property) as you would a normal server control.
5. After the user control is included in the Web Forms page, his public fields, properties, and methods are upgraded to the public properties (label properties) and methods of the control.
6. The user control participates in the entire lifecycle of each client request and handles its own events, encapsulating the page logic that partially contains his Web Forms page.
7, the user control cannot contain any form controls. If you want, you can use the form control in the Web Form page that contains him.
8, the user control can use the System.Web.UI.Page class's LoadControl method to use the program to create. The type of the user control is determined by the ClassName property of the Controls command.
9. For Web Forms pages that contain user controls, the strong type of the user control is available only if the Register command contains a user control (even if no user control label is actually declared).