While we were writing our web site, we could customize some of the controls to reuse them, in addition to using their fixed server controls.
Add a Web user control that lets you add content in front-end and behind the scenes, add a Web form, and drag Web user controls into the web The form corresponds to the place.
Note that the user control cannot be set as the Start page, and when dragged into a Web form, the Web form is changed to design mode before it can be dragged into success.
The following code is dragged in:
Front:
<%@ Register src= "Ww.ascx" Tagname= "ww" tagprefix= "uc1"%>// Generation Dock
Ww.ascx for user controls
<UC1:WW id= "WW1" runat= "server"/>// drag in the corresponding place
Accessing the properties of a user control
For example, aWeb form wants to get the value of a property of a user control.
User controls:
Background:
public string name = "Chen";
public string name{
get {return name;}
set {Name=value;}
}
Web Forms:
Front:
<%@ Register src= "Ww.ascx" Tagname= "ww" tagprefix= "uc1"%>// header
<UC1:WW id= "WW1" runat= "Server"/>
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
Background:
TextBox1.Text = This.ww1.name.ToString ();//Gets the name value of the user control, which is displayed on the TextBox control
To access a server control for a user control
such as a small example of login
User controls:
Front:
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
Background:
public string name{
get {return textbox1.text;}
set {TextBox1.Text = value;}
}
Web Forms:
Front:
<%@ Register src= "Ww.ascx" Tagname= "ww" tagprefix= "uc1"%>// header
<UC1:WW id= "WW1" runat= "Server"/>
<asp:button id= "Button1" runat= "Server" text= "button" onclick= "Button1_Click"/>
Background:
protected void Button1_Click (object sender, EventArgs e)
{
if (this.ww1.Name.ToString () = = "123")
{Response.Write ("<script>alert (' login successful ') </script>");}
else {Response.Write ("<script>alert (' Login failed ') </script>");}
}
ASP. NET Web User control