views:

126

answers:

2

hi I like to add Dynamically add WebUser Controls in a loop

like this con1 con2 con3 and more or less depending on the loop

is there a good way to do this

my first try look like this. but i don't know how to tell it to use the next one grpCon2

        foreach (DataRow Group in AllGroups.Rows)
    {
        GroupListControl grpCon1 = new GroupListControl();
        grpCon1.NickName = "STUFF";
        grpCon1.GroupName = "HARD";

        LiteralAddCOntrols.Text = @"<uc1:GroupListControl ID=""GrpCOn1"" runat=""server"" />";

    }
A: 

You can do that, but you have to remember two things:

  1. You have to give them ID - and remember them in a Session
  2. When the controls do any PostBack actions (like Click) - you have to refresh the exact collection on every post back in Page_PreInit event (which normaly the framework does) - because the attached event won't fire. And the Page_PreInit have to refresh the exact collection with the same ID-s.

It's possible but it's not so simple at the beginning.

And here is a detailed description how to do that.

http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

Krzysztof Król
+2  A: 

You need to use loadcontrol(pathtoyourusercontrol), and then and the control back to your page at the location you want.

sharedUC uc = (sharedUC)LoadControl("~/sharedUC/control.ascx");
plcContent.Controls.Add(uc);

Add : <%@ Reference Control="~/sharedUC/control.ascx" %>

To the page aspx loading the control and you will be able to use a typed reference to it.

RubbleFord