Public Class UserControl Inherits TemplateControl Implements IAttributeAccessor, INamingContainer, IUserControlDesignerAccessor
Dim instance As UserControl
public class UserControl : TemplateControl, IAttributeAccessor, INamingContainer, IUserControlDesignerAccessor
public ref class UserControl : public TemplateControl, IAttributeAccessor, INamingContainer, IUserControlDesignerAccessor
public class UserControl extends TemplateControl implements IAttributeAccessor, INamingContainer, IUserControlDesignerAccessor
The UserControl class is associated with files that have .ascx extensions. These files are compiled at run time as UserControl objects and cached in server memory.
You can nest user controls by declaring one .ascx file in another including the latter in a Web Forms page.
User controls are contained in ASP.NET Web Forms pages, and offer Web developers an easy way to capture commonly used Web UI. They are instantiated and cached in ways similar to Page objects. Unlike pages, however, user controls cannot be called independently. They can only be called from the page or other user control that contains them.
Derive from this class if you want to create a user control using the code-behind technique. This is recommended if you are developing Web Forms pages using this technique.
For information about creating user controls declaratively, see ASP.NET User Controls.
The class defined in the following example, SimpleControl, inherits the UserControl class and can be used as an ASP.NET code-behind class. It uses the TextBox, Label, and Button Web server controls and defines a myButton_Click method that assigns the TextBox.Text property value, along with two strings, to the Label.Text property.
Imports System Imports System.Web.UI Imports System.Web.UI.WebControls Public Class SimpleControl Inherits UserControl Public name As TextBox Public output As Label Public myButton As Button Public Sub myButton_Click(sender As Object, e As EventArgs) output.Text = "Hello, " + name.Text + "." End Sub End Class
using System; using System.Web.UI; using System.Web.UI.WebControls; public class SimpleControl:UserControl { public TextBox name; public Label output; public Button myButton; public void myButton_Click(object sender, EventArgs e) { output.Text = "Hello, " + name.Text + "."; } }
import System.*; import System.Web.UI.*; import System.Web.UI.WebControls.*; public class SimpleControl extends UserControl { public TextBox name; public Label output; public Button myButton; public void myButton_Click(Object sender, EventArgs e) { output.set_Text("Hello, " + name.get_Text() + "."); } //myButton_Click } //SimpleControl
The following example is markup contained in an .ascx file. You can use the SimpleControl class defined in the previous example as a code-behind class for the markup in this .ascx file.
<%@ control inherits = "SimpleControl" src = "SimpleControl.vb" %> <table style="background-color: yellow; font: 10pt verdana;border-width:1;border-style:solid;border-color:black;" cellspacing=15> <tr> <td><b>Enter your name here: </b></td> <td><ASP:TextBox id="name" runat="server"/></td> </tr> <tr> <td><b><ASP:Label id="output" runat="server"/></b></td> </tr> <tr> <td></td> <td><asp:button text="Submit" OnClick="myButton_Click" runat="server" /></td> </tr> </table>
<%@ control inherits = "SimpleControl" src = "SimpleControl.cs" %> <table style="background-color:yellow;font: 10pt verdana;border-width:1;border-style:solid;border-color:black;" cellspacing=15> <tr> <td><b>Enter your name here: </b></td> <td><ASP:TextBox id="name" runat="server"/></td> </tr> <tr> <td><b><ASP:Label id="output" runat="server"/></b></td> </tr> <tr> <td></td> <td><asp:button id="myButton" text="Submit" OnClick="myButton_Click" runat="server" /></td> </tr> </table>
<%@ control inherits = "SimpleControl" src = "SimpleControl.jsl" %> <table style="background-color:yellow;font: 10pt verdana;border-width:1;border-style:solid;border-color:black;" cellspacing=15> <tr> <td><b>Enter your name here: </b></td> <td><ASP:TextBox id="name" runat="server"/></td> </tr> <tr> <td><b><ASP:Label id="output" runat="server"/></b></td> </tr> <tr> <td></td> <td><asp:button id="myButton" text="Submit" OnClick="myButton_Click" runat="server" /></td> </tr> </table>
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.