How to: Create Instances of ASP.NET User Controls Programmatically
2012-04-28 11:33
686 查看
To create an instance of a user control programmatically
In the user control, be sure that the @ Control directive contains a ClassName attribute that assigns a class to the user control.
The following example sets the ClassName attribute to strongly type a user control.
<%@ Control className="MyUserControl" %>
In the page where you want to work with the user control, create a reference to the user control with the @ Reference directive.
When you create the user control programmatically, the strong type for your user control is available to the ASP.NET Web page only after you have created a reference to it. For example, the following code creates a reference to a user control created in the MyUserControl.ascx file.
<%@ Reference Control="MyUserControl.ascx" %>
Note |
---|
You use the @ Reference when you intend to load the control programmatically. You use the @ Register directive when you add a user control to the page declaratively. For details, see How to: Include a User Control in an ASP.NET Web Page. |
For example, if you want to create an instance of the user control declared as class Spinner, you use syntax such as the following:
VB
Protected Spinner1 As ASP.Spinner
C#
Protected ASP.Spinner Spinner1;
Create an instance of the user control in code by calling the LoadControl method.
Assign property values as necessary, and then add the control to the ControlCollection collection of a container on the page, such as a PlaceHolder control.
Note |
---|
When you add controls to the ControlCollection object using the Add method, they are placed in the collection in the order they are processed. If you want to add a control to a specific position in the collection, use the AddAt method and specify the index location where you want to store the control. |
<%@ Page Language="C#" %> <%@ Reference Control="~/Controls/Spinner.ascx" %> <script runat="server"> private ASP.Spinner Spinner1 protected void Page_Load(object sender, EventArgs e) { Spinner1 = (ASP.Spinner)LoadControl("~/Controls/Spinner.ascx"); } protected void Button1_Click(object sender, EventArgs e) { PlaceHolder1.Controls.Add(Spinner1); } </script> <html> <head id="Head1" runat="server"> <title>Load User Control Programmatically</title> </head> <body> <form id="form1" runat="server"> <div> <asp:PlaceHolder runat="server" ID="PlaceHolder1" /> <br /> <asp:Button ID="Button1" runat="server" Text="Click to Add User Control" OnClick="Button1_Click" /> <br /> <br /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </div> </form> </body> </html>
reference:http://msdn.microsoft.com/en-us/library/c0az2h86(v=vs.100).aspx
相关文章推荐
- ASP.NET 成员资格 Part.4(ChangePassword、CreateUserWizard)
- FW: How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
- How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
- ASP.NET关于Login控件使用,LoginView 控件,CreateUserWizard 控件
- In Search Of ASP.Net Controls
- 海洋工作室——网站建设专家:How To: Create an ASP.NET AJAX Style Folder Explorer
- asp.net控件前缀规范(Prefix guideline of asp.net controls)
- Building ASP.NET Server Controls-Copy of Pager.cs
- An Overview Of The New Services, Controls, And Features In ASP.NET 2.0
- ASP.NET User Controls and Custom Server Controls
- Supporting Templates with ASP.NET User Controls
- Supporting Templates with ASP.NET User Controls
- Code snippet of the day : Resizing ASP.NET Datagrid Controls in Edit mode at runtime
- The New Controls of ASP.NET 2.0
- Complete Lifecycle of an ASP.Net page and controls
- How to Add ASP.NET User Controls To Master Pages or Page Layouts
- Pass data from a ASP.NET page to ASCX user controls loaded dynamically
- How To: Create an ASP.NET AJAX Style Folder Explorer
- Advantages and disadvantages of ASP.NET Server Controls and HTML Server Controls
- Packaging ASP.NET User Controls into an Assembly and Reuse it Multiple Projects