What is the user control used to do?
User controls are often used to unify the style of Web page display.
about user Controls
1, the user control has an. ascx extension.
2, the user control does not have the @page instruction, but contains the @control instruction.
3. User controls cannot be run as stand-alone files, you must create ASP.net pages and add them later.
4, you can use the same XHTML elements and Web server controls on the user control. For example, a button can be placed in a user control and the event handling of a button is created.
Creating a User control
Select the user control in the new item in VS. (You can also convert a single file into a user control.) )
Make a user control.
For example:
User Control Code:
Webusercontro.ascx Code:
<%@ control language= "C #" autoeventwireup= "true" codefile= "WebUserControl.ascx.cs" inherits= "Webusercontrol"% >
<asp:textboxidasp:textboxid= "txtsearch" runat= "Server" ></asp:TextBox>
<br/>
C # code:
Public Partialclass WebUserControl:System.Web.UI.UserControl
{
//displayed in text.
protected void btnSearch_Click (Objectsender, EventArgs e)
{
Txtsearch.text = "Search complete";
}
Adds the public property text for the user control.
private string _text;
public string Text
{
get
{
_text;
}
Set
{
_text = value;
}
}
Assigns a value to the control's Text property.
protected void Page_Load (Objectsender, EventArgs e)
{
Btnsearch.text = this. Text;
}
To use a user control:
Drag the ascx file directly to the page we want to use.
The automatically generated code is as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "UserControlTest.aspx.cs" inherits= "Usercontroltest"%>
<%@ Register src= "~/webusercontrol.ascx" tagname= "Webusercontrol" tagprefix= "uc1"%>
<! DOCTYPE htmlpublic "-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Show
The difference between the user control and the previous one, I think if your page is almost entirely with a control, and a lot of duplicate pages, we can take the user control.
The above is about how to use the full content of ASP.net user control, I hope to be proficient in user control help.