In the above code cntrlHolder is a PlaceHolder/Panel. I need to add more controls of type WebUserControl into the same page. How can this be done?
The page adds only one instance of the control. Any suggestions??
I'd probably give each instance you create a unique id, as the current method looks like its overwriting them. You can do that by setting the UserControl.ID property before adding the control to the container
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:
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.
Last edited by hollystyles; Feb 2nd, 2007 at 4:55 am.
Thanks.. a lot for explaining that. I am new with ASP.NET. That's why i didn't understand why the second control won't show up when i press the add control button. But now i understand a bit.
Thanks a lot once again for this. But since i'm new with ASP.NET, a lot of questions are bound to come your way. I hope you'll answer them without hesitation like you did this one.
I read carefully your post but there is still an issue for me if you want to loop. I think I'm doing something wrong but I really can not find where :
For Each ...
SqlDataSource1.SelectCommand = "select [note] from [NOTES] where [id_note]='" & MyVar & "'"
Dim mycontrol As Control = LoadControl("tata.ascx")
panel.ContentContainer.Controls.Add(mycontrol)
Next
Right, for each loop a control (from tata.ascx) should load into my panel ... and its the case.
However my control in tata.ascx is a detailsview bound to SqlDataSource1, so I presume that all controls should have different values according to 'MyVar' which is changing at every loop.
My problem is that all the detailsview (in readonly or edit mode) have exactly the same value. (Label1 shows always the same value for example).
This value is the last one according to the loop.
I've spend a full day on this without success and I don't have any more ideas. My understanding of this must be poor and it would be very nice to have your comments.
Thank you and I hope I have been clear enough as English is not my mother tongue.
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.