UserControl Class
Represents an .ascx file, also known as a user control, requested from a server that hosts an ASP.NET Web application. The file must be called from a Web Forms page or a parser error will occur.
For a list of all members of this type, see UserControl Members.
System.Object
System.Web.UI.Control
System.Web.UI.TemplateControl
System.Web.UI.UserControl
[Visual Basic] Public Class UserControl Inherits TemplateControl Implements IAttributeAccessor, IUserControlDesignerAccessor [C#] public class UserControl : TemplateControl, IAttributeAccessor, IUserControlDesignerAccessor [C++] public __gc class UserControl : public TemplateControl, IAttributeAccessor, IUserControlDesignerAccessor [JScript] public class UserControl extends TemplateControl implements IAttributeAccessor, IUserControlDesignerAccessor
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
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 Web Forms User Controls.
Example
[Visual Basic, C#, C++] 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.
[Visual Basic] 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 'myButton_Click End Class 'SimpleControl [C#] 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 + "."; } } [C++] #using <mscorlib.dll> #using <System.Web.dll> #using <System.dll> using namespace System; using namespace System::Web::UI; using namespace System::Web::UI::WebControls; public __gc class SimpleControl:public UserControl { public: TextBox* name; Label* output; Button* myButton; void myButton_Click(Object* /*sender*/, EventArgs* /*e*/) { output->Text = String::Format( S"Hello, {0}.", name->Text ); } };
[Visual Basic, C#, C++] The following example is markup contained in a .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.
[Visual Basic] <%@ 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> [C#] <%@ 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>
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Web.UI
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
Assembly: System.Web (in System.Web.dll)
See Also
UserControl Members | System.Web.UI Namespace | TemplateControl | IAttributeAccessor | Page | Web Forms User Controls