944,184 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 5255
  • ASP.NET RSS
Feb 1st, 2007
0

Using UserControls in ASP.NET

Expand Post »
I have to dynamically add user controls to a asp.net web page. I know that can be done as follows:

C# Syntax (Toggle Plain Text)
  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??
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shAq is offline Offline
10 posts
since Jul 2005
Feb 1st, 2007
0

Re: Using UserControls in ASP.NET

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
Reputation Points: 68
Solved Threads: 9
Junior Poster
sedgey is offline Offline
130 posts
since Jan 2007
Feb 1st, 2007
1

Re: Using UserControls in ASP.NET

Quote ...
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

ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Feb 1st, 2007
0

Re: Using UserControls in ASP.NET

hollystyles is spot on
Reputation Points: 68
Solved Threads: 9
Junior Poster
sedgey is offline Offline
130 posts
since Jan 2007
Feb 1st, 2007
0

Re: Using UserControls in ASP.NET

Quote ...
hollystyles is spot on
Thankyou, that feels good coming from an MCSD, I'm part way through getting MCTS.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Feb 1st, 2007
0

Re: Using UserControls in ASP.NET

hehe, good luck with that!
Reputation Points: 68
Solved Threads: 9
Junior Poster
sedgey is offline Offline
130 posts
since Jan 2007
Feb 1st, 2007
0

Re: Using UserControls in ASP.NET

No ASP.NET will automatically give them unique ID's, you just need a loop

ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shAq is offline Offline
10 posts
since Jul 2005
Feb 2nd, 2007
0

Re: Using UserControls in ASP.NET

Quote ...
How is this different from giving the command to create the control twice?
It's not that's what we are doing.
ASP.NET Syntax (Toggle Plain Text)
  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:

ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Feb 2nd, 2007
0

Re: Using UserControls in ASP.NET

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shAq is offline Offline
10 posts
since Jul 2005
Feb 13th, 2008
0

Re: Using UserControls in ASP.NET

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
boisellea is offline Offline
1 posts
since Feb 2008

This thread is solved

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.
Message:
Previous Thread in ASP.NET Forum Timeline: good way of validating login info vs SQL D/B
Next Thread in ASP.NET Forum Timeline: datatype TEXT in stored procedure





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC