ASP.NET Web Forms Web Forms Server Controls Web Forms User Controls
You can create a user control declaratively by using a text or HTML editor. User control declarative syntax is very similar to syntax used to create a Web Forms page; the primary difference is that user controls do not include the <html
>, <body
>, and <form
> elements around the content. Include these elements in the Web Forms pages that will contain the user control.
User controls can be as simple as a text file, or they can contain other ASP.NET server controls. The following procedure outlines a simple logon form that you can include on multiple pages of an application.
- Open a text or an HTML editor and create a code-declaration block that exposes the properties, event handlers, and any other code that you want to include in the user control’s functionality.
You have two options when using properties of user controls. First, you can define new properties for the user control and manipulate them. Second, you can manipulate the properties of the server controls that constitute the user control. For example, if you declare a TextBox Web server control in a user control, and gave it an ID of myTextbox
, you can manipulate its Text property by using myTextbox.Text
syntax.
NOTE: All properties and methods of any ASP.NET server controls contained in a user control are promoted to public properties and methods of the user control when it is included in a Web Forms page.
The following code example declares UserId and Password properties that map to the text boxes in the code in Step 2. You can manipulate these properties, declaratively or programmatically, in any Web Forms page that contains this user control.
<script language="C#" runat="server">
public String UserId {
get {
return User.Text;
}
set {
User.Text = value;
}
}
public String Password {
get {
return Pass.Text;
}
set {
Pass.Text = value;
}
}
</script>
<script language="VB" runat="server">
Public Property UserId ( ) As String
Get
Return User.Text
End Get
Set
User.Text = value
End Set
End Property
Public Property Password ( ) As String
Get
Return Pass.Text
End Get
Set
Pass.Text = value
End Set
End Property
</script> |
|
C# |
VB |
- Create the UI elements that you want the user control to display. For example, the following code creates the logon form that interacts with the code from Step 1.
<table>
<tr>
<td><b>Login: </b></td>
<td><asp:TextBox id="User" runat="server"/></td></tr>
<tr>
<td><b>Password: </b></td>
<td><asp:TextBox id="Pass" TextMode="Password" runat="server"/></td></tr>
<tr>
<td></td>
<td><asp:Button Text="Click Here"
OnClick="SubmitBtn_Click" runat="server"/></td></tr>
<tr>
<td ColSpan=2><asp:Label id="myLabel" runat="server"/></td></tr>
</table>
- Name the user control and save it with an .ascx file name extension. For example, you can name the code in this procedure Login.ascx.
To use this file as a control, see Including a User Control in a Web Forms Page. To specify a method that will process the form, see Defining Web Forms Event-Handling Methods.