Export (0) Print
Expand All

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.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class UserControl : TemplateControl, 
	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.

TopicLocation
How to: Cache Multiple Versions of a User Control Based on ParametersBuilding ASP .NET Web Applications
How to: Convert Web Forms Pages into ASP.NET User ControlsBuilding ASP .NET Web Applications
How to: Treat a User Control as a Web Parts ControlBuilding ASP .NET Web Applications
How to: Cache Multiple Versions of a User Control Based on ParametersBuilding ASP .NET Web Applications
How to: Convert Web Forms Pages into ASP.NET User ControlsBuilding ASP .NET Web Applications
How to: Treat a User Control as a Web Parts ControlBuilding ASP .NET Web Applications
How to: Create ASP.NET User Controls (Visual Studio)Building ASP .NET Web Applications in Visual Studio
How to: Include ASP.NET User Controls in Web Pages (Visual Studio)Building ASP .NET Web Applications in Visual Studio

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 noteSecurity 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.

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.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>

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

Community Additions

ADD
Show:
© 2014 Microsoft