| | |
Using UserControls in ASP.NET
Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2005
Posts: 10
Reputation:
Solved Threads: 0
I have to dynamically add user controls to a asp.net web page. I know that can be done as follows:
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??
C# Syntax (Toggle Plain Text)
WebUserControl webug = (WebUserControl)Page.LoadControl("WebUserControl.ascx"); cntrlHolder.Controls.Add(webug);
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
•
•
•
•
I'd probably give each instance you create a unique id, as the current method looks like its overwriting them
ASP.NET Syntax (Toggle Plain Text)
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.
Last edited by hollystyles; Feb 1st, 2007 at 10:29 am.
•
•
•
•
hollystyles is spot on
•
•
Join Date: Jul 2005
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
No ASP.NET will automatically give them unique ID's, you just need a loop
ASP.NET Syntax (Toggle Plain Text)
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.
Last edited by shAq; Feb 1st, 2007 at 6:50 pm.
•
•
•
•
How is this different from giving the command to create the control twice?
ASP.NET Syntax (Toggle Plain Text)
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:
ASP.NET Syntax (Toggle Plain Text)
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.
Last edited by hollystyles; Feb 2nd, 2007 at 4:55 am.
•
•
Join Date: Jul 2005
Posts: 10
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Feb 2008
Posts: 1
Reputation:
Solved Threads: 0
Hi all,
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.
Here is what I have in my tata.ascx page:
<asp:detailsview id="DetailsView1" runat="server" datasourceid="SqlDataSource3" autogeneraterows="False">
<Fields>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("note") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("note") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:detailsview>
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.
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.
Here is what I have in my tata.ascx page:
<asp:detailsview id="DetailsView1" runat="server" datasourceid="SqlDataSource3" autogeneraterows="False">
<Fields>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("note") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("note") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:detailsview>
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.
![]() |
Similar Threads
- ASP.NET Tutorial: Caching in ASP.NET (ASP.NET)
- ASP.NET and Mozilla - Issues! (ASP.NET)
- Question for ASP.NET/Mozilla Users...or anyone... (ASP.NET)
- .NET Speech SDK for ASP.NET (ASP.NET)
Other Threads in the ASP.NET Forum
- Previous Thread: good way of validating login info vs SQL D/B
- Next Thread: datatype TEXT in stored procedure
| Thread Tools | Search this Thread |
.net activexcontrol advice ajax alltypeofvideos appliances asp asp.net bc30451 beginner bottomasp.net box browser button c# cac checkbox click commonfunctions control css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock deployment development dgv dialog dropdownlist dynamic dynamically edit embeddingactivexcontrol expose fileuploader fill findcontrol flash formatdecimal formview gridview gudi iframe iis javascript listbox login microsoft mono mouse mssql multistepregistration news novell numerical objects opera panelmasterpagebuttoncontrols radio redirect registration relationaldatabases reportemail rotatepage save schoolproject search security sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 ssl suse textbox tracking treeview unauthorized validatedate validation vb.net video videos virtualdirectory vista visualstudio web webapplications webdevelopemnt webdevelopment webprogramming webservice xsl youareanotmemberofthedebuggerusers






