1

Visual Studio 2008, C#, ASP.NET

Need to make multiple copies (or instances) of a web user control programmatically.

I have UserControlFile.ascx which contains an asp:Label with ID="vhLabel" in it. In that file I set label's Text property through public string vhText variable, as usual:

public string vhText
{
    set
    {
        vhLabel.Text = value;
    }
}

There are also some divs and other html data in this file that cannot be created in C# programmatically, so I have to use ascx file.

Now, in code-behind file I need to:

  1. clone this user control many times;
  2. set unique value to label's property "Text" of each clone through "vhText" variable.

Please, share your suggestions on these issues. If you need, I can show my code here. I've been looking for an answer for very long, but still unsuccessful.

2 Answers 2

1

You can find a walkthrough of how to programmatically add user controls to your page here: http://msdn.microsoft.com/en-us/library/c0az2h86(v=vs.100).aspx

In your case, you need something like this in your UserControlFile.ascx control (the className attribute is the important bit):

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControlFile.ascx.cs" Inherits="UserControlFile" className="MyUserControl" %>
<asp:Label ID="vhLabel" runat="server"></asp:Label>

Then, at the top of your page that will include the control (change the file path appropriate to your structure):

<%@ Reference Control="~/Controls/UserControlFile.ascx" %>

Finally, the code behind of the page will contain the code to programmatically add the instances of the control via the LoadControl method and the use of the MyUserControl type - defined in the className attribute in the @Control directive above:

protected void Page_Load(object sender, EventArgs e)
{
    var control = (ASP.MyUserControl)LoadControl("~/Controls/UserControlFile.ascx");
    control.vhText = "1";
    Page.Controls.Add(control);

    control = (ASP.MyUserControl)LoadControl("~/Controls/UserControlFile.ascx");
    control.vhText = "2";
    Page.Controls.Add(control);

    /* etc... */
}

That should point you in the right direction...

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, thank you, it finally works. Can you help me now with one minor thing. I don't see a way to programatically create any number of these controls. I usually used constructions like "myLabel = new Label" and then added them to TableCells as controls. How can I do it with this my user control?
myLabel = new Label would be replaced by the var control... line above, e.g. var myLabel = (ASP.MyUserControl)LoadControl("~/Controls/UserControlFile.ascx"); and then add the control to your TableCell as normal.
0

You need to use LoadControl method:

MyUserControl control = (MyUserControl)Page.LoadControl("~/UserControlFile.ascx");
control.vhText = "first control";
Page.Controls.Add(control);

control = (MyUserControl)Page.LoadControl("~/UserControlFile.ascx");
control.vhText = "second control";
Page.Controls.Add(control);

control = (MyUserControl)Page.LoadControl("~/UserControlFile.ascx");
control.vhText = "third control";
Page.Controls.Add(control);

1 Comment

@Vladimir you must have such line somewhere: public partial class [something here] : System.Web.UI.UserControl so just replace "MyUserControl" from the above sample code with whatever is the actual class name you see instead of "something here". Make sure the control code behind is using the same namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.