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.
Namespace:
System.Web.UI
Assembly:
System.Web (in System.Web.dll)
Visual Basic (Declaration)
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class UserControl _
Inherits TemplateControl _
Implements IAttributeAccessor, INamingContainer, IUserControlDesignerAccessor
Dim instance As UserControl
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class UserControl : TemplateControl,
IAttributeAccessor, INamingContainer, IUserControlDesignerAccessor
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
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.
Security Note: |
|---|
This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview. |
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 + ".";
}
}
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>
System..::.Object
System.Web.UI..::.Control
System.Web.UI..::.TemplateControl
System.Web.UI..::.UserControl
System.Web.UI..::.MasterPage
System.Web.UI.MobileControls..::.MobileUserControl
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Reference
Other Resources