Using UserControls in ASP.NET

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2005
Posts: 10
Reputation: shAq is an unknown quantity at this point 
Solved Threads: 0
shAq shAq is offline Offline
Newbie Poster

Using UserControls in ASP.NET

 
0
  #1
Feb 1st, 2007
I have to dynamically add user controls to a asp.net web page. I know that can be done as follows:

  1. WebUserControl webug = (WebUserControl)Page.LoadControl("WebUserControl.ascx");
  2. cntrlHolder.Controls.Add(webug);
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??
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 130
Reputation: sedgey is on a distinguished road 
Solved Threads: 8
sedgey's Avatar
sedgey sedgey is offline Offline
Junior Poster

Re: Using UserControls in ASP.NET

 
0
  #2
Feb 1st, 2007
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
David Ridgway: so little daylight, too much caffeine
MCSD MCAD MCSE
http://web2asp.net
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Using UserControls in ASP.NET

 
1
  #3
Feb 1st, 2007
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

  1. for(int i = 0; i < 2; i++)
  2. {
  3. WebUserControl webug = (WebUserControl)Page.LoadControl("WebUserControl.ascx");
  4. cntrlHolder.Controls.Add(webug);
  5. }

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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 130
Reputation: sedgey is on a distinguished road 
Solved Threads: 8
sedgey's Avatar
sedgey sedgey is offline Offline
Junior Poster

Re: Using UserControls in ASP.NET

 
0
  #4
Feb 1st, 2007
hollystyles is spot on
David Ridgway: so little daylight, too much caffeine
MCSD MCAD MCSE
http://web2asp.net
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Using UserControls in ASP.NET

 
0
  #5
Feb 1st, 2007
hollystyles is spot on
Thankyou, that feels good coming from an MCSD, I'm part way through getting MCTS.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 130
Reputation: sedgey is on a distinguished road 
Solved Threads: 8
sedgey's Avatar
sedgey sedgey is offline Offline
Junior Poster

Re: Using UserControls in ASP.NET

 
0
  #6
Feb 1st, 2007
hehe, good luck with that!
David Ridgway: so little daylight, too much caffeine
MCSD MCAD MCSE
http://web2asp.net
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 10
Reputation: shAq is an unknown quantity at this point 
Solved Threads: 0
shAq shAq is offline Offline
Newbie Poster

Re: Using UserControls in ASP.NET

 
0
  #7
Feb 1st, 2007
Originally Posted by hollystyles View Post
No ASP.NET will automatically give them unique ID's, you just need a loop

  1. for(int i = 0; i < 2; i++)
  2. {
  3. WebUserControl webug = (WebUserControl)Page.LoadControl("WebUserControl.ascx");
  4. cntrlHolder.Controls.Add(webug);
  5. }

What's important here is we are instantiating a NEW instance of the control each time, not appending the same one.
How is this different from giving the command to create the control twice?
Last edited by shAq; Feb 1st, 2007 at 6:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Using UserControls in ASP.NET

 
0
  #8
Feb 2nd, 2007
How is this different from giving the command to create the control twice?
It's not that's what we are doing.
  1. MyUserControl control = (MyUserControl)Page.LoadControl("MyUserControl.ascx");
  2.  
  3. PlaceHolder1.Controls.Add(control);
  4. 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:

  1. for (int i = 0; i < 2; i++)
  2. {
  3. MyUserControl control = (MyUserControl)Page.LoadControl("MyUserControl.ascx");
  4.  
  5. PlaceHolder1.Controls.Add(control);
  6. }

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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 10
Reputation: shAq is an unknown quantity at this point 
Solved Threads: 0
shAq shAq is offline Offline
Newbie Poster

Re: Using UserControls in ASP.NET

 
0
  #9
Feb 2nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1
Reputation: boisellea is an unknown quantity at this point 
Solved Threads: 0
boisellea boisellea is offline Offline
Newbie Poster

Re: Using UserControls in ASP.NET

 
0
  #10
Feb 13th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC