Web Forms User Control
User control introduction
In addition to the built-in server control provided by ASP.NET, you can also use the production of the Web Forms page already mastered.
Technology, easy to define your own controls. In fact, just change it slightly, almost any Web Forms page
Can be used as a server control, reuse it on other pages. (Note that the type of user control is System.Web.ui.u
SERCONTROL, it is inherited from System.Web.ui.Control). Web Forms page is used as a server
When the control is used, referred to as the user control. After the transition, usually use the .ascx extension to indicate the user control. This
Just ensure that the user control file cannot be executed as a separate web form page (you can understand, in user controls)
There is a little between the Web Forms page, but it is very important. User control uses register (registration)
Order is included in the Web Forms page:
TagPRefix determines a unique namespace for the user control. (This, multiple user controls with the same name can
Mutual distinguishing). The value of TAGNAME is the only name of the user control (you can use any name). SRC attribute is used
The virtual path of the household control, such as "mypagelet.ascx" or "/myapp/include/mypagelet.ascx".
After registering the user control, you can place the User Control Label in the Web Forms page, just like a usual server.
The control is the same (including the runat = "server" attribute):
The following example shows the case where the Web Forms page introduces the user control. The user control in this example is just a simple
Static file.
C # Pagelet1.aspx
[Run] | [Source File]
Extend the properties of user controls
When the Web Forms page is handled as a user control, the public field and method of the web forms page are upgraded to public
Properties (ie label properties) and public methods (methods for labels). The following example will be in the previous example user control
The line extends, adds two public String fields. Note that these two fields can be directly declared in the included page, or
Control with procedures.
C # Pagelet2.aspx
[Run] | [Source File]
In addition to upgrading the public field to the control attribute, the attribute syntax can also be used. Use the attribute syntax to win for setting or
Once the attribute value is obtained, execute the code. The following example demonstrates an Address user control, he binds TextBox
The TEXT attribute of the 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 including the Web Forms page, which are "Billing Address" and
"Shipping Address" sets the capensation property. The truly power of the user control is this reuse.
C # Pagelet3.aspx
[Run] | [Source File]
Another useful thing for user controls is the login control, used to collect usernames and passwords.
C # Pagelet4.aspx
[Run] | [Source File]
In this example, the server verification control is added to the Login user control.
C # Pagelet5.aspx
[Run] | [Source File]
Package event in user control
Like a normal server control, user controls participate in the life cycle of the entire request. This means that users
Controls can operate their own events, encapsulate certain page logic from the web forms page.
The following example demonstrates a product list user control that handles your submit back. Pay attention to user control
The piece itself is not bound
Control. Because only one form control appears on the page (ASP)
.NET does not allow the nested server form), so it is left to define him to include the Web Forms page.
C # Pagelet6.aspx
[Run] | [Source File]
Create user controls with programs
Like the normal server control, the user control is the same as the program creation. Page LoadControl method used
Load user controls while passing the virtual path to the source file of the user control.
Control C1 = loadingControl ("Pagelet7.ascx");
(Pagelet7cs) C1) .category = "business";
Page.Controls.Add (C1);
The type of user control is determined by the ClassName property of the control command. For example, the following example, a file
User controls saved as "Pagelet7.ascx" are assigned to "Pagelet7CS" strong type:
Since the type of loadControl method is system.web.ui.control, you have a user control.
Not attribute, it must throw appropriate strongest types. Finally, the user control is added to the basic page for ControlCollectio
N-collection.
C # Pagelet7.aspx
[Run] | [Source File]
Important For the Web Forms page containing the user control, only when the register command contains user controls (
Even if there is no user control tag being declared), the powerful type of user control is available.
chapter summary
1. User control allows developers to create a customized custom creation using the programming technology used to create the Web Forms page.
Yi control.
2. As a difference with the normal page, use the .ascx file extension to indicate the user control. This guarantees user controls
Can not be performed like a standard Web Forms page.
3. Use the register command to include the user control in the Web Forms page. The register command specifies TAGP
Refix, Tagname, and SRC Location.
4. After registering the user control, you can use the user control like a normal server control (containing Runat).
= "Server" attribute).
5. User control is included in the Web Forms page, his public field, attributes, and methods are upgraded to the control of the control.
A total property (label attribute) and method.
6. User controls participate in the entire life cycle of each client request, and dealing with its own events, packaged some of the included
The page logic of his web forms page.
7. User controls cannot contain any FORM controls. If you need, you can use it in the web form page that contains his Web Form.
FORM control.
8. User controls can be created with the LoadControl method for the System.Web.ui.page class. User control
The type is determined by the CLASSNAME attribute of the control command.
9. For the Web Forms page containing the user control, only when the register command contains user controls (even if there is no
There is a user control tag that is actually declared), and the powerful type of user control is available.