Working with UserControls Programatically

  • Thread starter Thread starter Andrea Williams
  • Start date Start date
A

Andrea Williams

Does anyone know of some in depth documentation regarding how to add user
controls programatically? I have a User Control, which has some basic html
controls, and depending on the number selected in an HtmlSelect control's
value, I need to put x number of forms (or usercontrol) on the page.

I've added the control once to the ASPX, but how do I add more than one when
needed to the page, then how do I rename the form names so that they are
unique to that particular instance of that control?

Any help and documentation would be appreciated!

Thanks!
Andrea
 
Hi Andrea,

The TemplateControl.LoadControl method is used to load usercontrols at
runtime. Then you need to insert the control into some other control
(parent) by calling the parent's Controls.Add method.

Not sure what exactly did you mean by "forms" but keep in mind that you can
only have one server-side form per page.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx

To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
 
One server-side form per aspx, right? yes... The first question I ask my
users is how many? They select a number from a drop down and then I want to
provide up to six groups of the same field. How many of those field
groupings depends on the number they select. My field grouping is in a User
Control that I made and the ASPX needs to include up to six of them on the
ASPX page inbetween the Form tags. Make sense?

So how do I do it... In the ASP world, I would loop from 1 to the number
selected and write out the extra fields to the page. How do I do that in
ASP.Net?

Andrea
 
It makes sense. Use the LoadControl method that is available in your
Page-derived class to load as many UC as you want. The server-side form
should be defined in the aspx and not in the UC. After loading UC you need
to insert them into the Form Controls collection. In code this should look
like:
[C#]
Control form = FindControl ("YourFormID");
form.Controls.Add (LoadControl ("YourUserControlID"));

you can put the 2nd line in a loop that executes n times based on some user
selection, you could also vary the user control id if needed.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
 
Ok, that sounds simple enough... so when I'm getting the data after the
form is posting, is there any way for me to rename the field name_1, name_2,
etc so that I can easily find the data I'm looking for after the post. Some
of the fileds are going to be radio buttons and so each groupings name need
to be unique so that I do get the data mixed up and so that ht radio button
work properly. I hope this makes sense.

Is there a simple way to do that??

Thanks, BTW! This is helping!
Andrea


Victor Garcia Aprea said:
It makes sense. Use the LoadControl method that is available in your
Page-derived class to load as many UC as you want. The server-side form
should be defined in the aspx and not in the UC. After loading UC you need
to insert them into the Form Controls collection. In code this should look
like:
[C#]
Control form = FindControl ("YourFormID");
form.Controls.Add (LoadControl ("YourUserControlID"));

you can put the 2nd line in a loop that executes n times based on some user
selection, you could also vary the user control id if needed.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

Andrea Williams said:
One server-side form per aspx, right? yes... The first question I ask my
users is how many? They select a number from a drop down and then I
want
to
provide up to six groups of the same field. How many of those field
groupings depends on the number they select. My field grouping is in a User
Control that I made and the ASPX needs to include up to six of them on the
ASPX page inbetween the Form tags. Make sense?

So how do I do it... In the ASP world, I would loop from 1 to the number
selected and write out the extra fields to the page. How do I do that in
ASP.Net?

Andrea

you
can
 
Back
Top