I'd probably give each instance you create a unique id, as the current method looks like its overwriting them
No ASP.NET will automatically give them unique ID's, you just need a loop
for(int i = 0; i < 2; i++)
{
WebUserControl webug = (WebUserControl)Page.LoadControl("WebUserControl.ascx");
cntrlHolder.Controls.Add(webug);
}
What's important here is we are instantiating a NEW instance of the control each time, not appending the same one.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
hollystyles is spot on
Thankyou, that feels good coming from an MCSD, I'm part way through getting MCTS.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
How is this different from giving the command to create the control twice?
It's not that's what we are doing.
MyUserControl control = (MyUserControl)Page.LoadControl("MyUserControl.ascx");
PlaceHolder1.Controls.Add(control);
PlaceHolder1.Controls.Add(control);
The above creates a reference to a new instance of the user control and then adds the SAME reference twice, the Add method will see that reference already exists in its collection so is discarded. If we do this:
for (int i = 0; i < 2; i++)
{
MyUserControl control = (MyUserControl)Page.LoadControl("MyUserControl.ascx");
PlaceHolder1.Controls.Add(control);
}
We are creating a NEW reference to a NEW instance in each loop, so you get 2 user controls appearing in the page. Right click in your web browser and choose to view source. See how ASP.NET has created unique names for the controls.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68