- user controls: using for specific programs
- example: User Control Login
- vs Span lang= "ZH-CN" style= "font-size:12pt" to create the program, as follows
- open UserControl.ascx
- Open UserControl.ascx under the UserControl.ascx.cs, write the following code:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.Web.UI.WebControls; Namespace usercontrol{publicpartial class UserControl:System.Web.UI.UserControl {protected void Page_Load ( Object sender, EventArgs e) {} publicstring txtusername//attribute 1: User name {Set {This.txtName.Text = value; } get {return this.txtName.Text; }} publicstring txtuserpwd//property 2: Password {set {This.txtPwd.Text = V Alue; } get {return this.txtPwd.Text; }}//control event protected void Btnlogin_click (object sender, EventArgs e) {if (Txtuserna me = = "Liuying") && (txtuserpwd = = "Liuying")) {Response.Write ("login Successful"); } else{Response.Write ("Login Failed"); } } }}
- will be WebForm1 Set as the startup form, and then open WebForm1.aspx, switch to the design window, drag the user control into the page, Ctrl+f5 , start the program, enter a string in the text box, click Login, the interface will prompt the login success or failure.
- There are many ways to assign a property to a user control, and the above is a method of typing in a text box, and there is one way toWebForm1direct assignment in the user interface code form
- <uc1:usercontrol id= "Logincontrol" txtusername= "liuying" txtuserpwd= "liuying" runat= "Server"/>
- and in WebForm1 in the user interface design form, right-click the user control, select Properties
- Custom Controls: Custom controls are global, as long as they are set, all use this Visual Studio users can use the
- Example: namespaces are not very cumbersome to reference, and we often forget which class libraries should be referenced in a particular environment, such as D the namespace of the layer
- Whether the custom control or the user control, will give us the way of programming convenience, learn to use the two is not difficult, the best use is difficult!
Powerful ASP. NET control----user control against custom controls