Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - dynamic creation of user controls

 
Thread Tools Search this Thread
Old 12-30-2003, 11:59 AM   #1
Default dynamic creation of user controls


Hi, I wonder if anyone can tell me how to add user controls to a page
dynamically?

In context: I have a bunch of HTML tables which due to time
constraints can't be populated to a database, so I was planning on
cutting the HTML source and creating a user control from said source.
I will then populate the relevant database for the product with the
name of the user control, so if someone selects product "blah" the
query returns the name of the relevant control with other details.

How then might I add this code to the page, would I better off using
the old style <% -- inline code here -- %> for example
<uc1:<%=some_control_name%> id=" <%=some_control_name%>"
runat="server"> in the aspx web form or by using code-behind?

And would I have register each control that might be used on the page
too?

<%@ Register TagPrefix="uc1" TagName="cb4Way" Src="pecs/cb4Way.ascx"%>
<%@ Register TagPrefix="uc1" TagName="pecs/tubingNTTPec.ascx"

TIA


Adie
  Reply With Quote
Old 12-30-2003, 12:34 PM   #2
Hans Kesting
 
Posts: n/a
Default Re: dynamic creation of user controls


"Adie" <> wrote in message
news: s.com...
> Hi, I wonder if anyone can tell me how to add user controls to a page
> dynamically?
>


There is a LoadControl method that takes a relative path to an ascx as
parameter.

Use a PlaceHolder control where you want your dynamic controls to appear
( say "PlaceHolder1"), then add your control to it:
PlaceHolder1.Controls.Add(LoadControl(relativeASCX Path));


Hans Kesting


  Reply With Quote
Old 12-30-2003, 01:54 PM   #3
Adie
 
Posts: n/a
Default Re: dynamic creation of user controls

Hans Kesting wrote:
>"Adie" <> wrote in message
>>
>> Hi, I wonder if anyone can tell me how to add user controls to a page
>> dynamically?

>
>There is a LoadControl method that takes a relative path to an ascx as
>parameter.
>
>Use a PlaceHolder control where you want your dynamic controls to appear
>( say "PlaceHolder1"), then add your control to it:
>PlaceHolder1.Controls.Add(LoadControl(relativeASC XPath));


Excellent, thank you Hans.
  Reply With Quote
Old 12-30-2003, 02:14 PM   #4
Kevin Spencer
 
Posts: n/a
Default Re: dynamic creation of user controls

> There is a LoadControl method that takes a relative path to an ascx as
> parameter.


In addition, if you want to have some control over the User Control after
loading it, the LoadControl() method returns a reference to the Control you
added. Also, note that when you dynamically add any Control to a WebForm,
you must re-add it with each PostBack in order for it to "remain" on the
page across PostBacks.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Hans Kesting" <> wrote in message
news:#...
>
> "Adie" <> wrote in message
> news: s.com...
> > Hi, I wonder if anyone can tell me how to add user controls to a page
> > dynamically?
> >

>
> There is a LoadControl method that takes a relative path to an ascx as
> parameter.
>
> Use a PlaceHolder control where you want your dynamic controls to appear
> ( say "PlaceHolder1"), then add your control to it:
> PlaceHolder1.Controls.Add(LoadControl(relativeASCX Path));
>
>
> Hans Kesting
>
>



  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump