Create User Controls dynamically

D

David Lozzi

Howdy,

I have a user control that needs to be displayed X amount of times on one
page. How is this done? I was thinking of putting it into a datagrid, which
would work, but is there a better way?

Thanks!
 
S

Steven Cheng[MSFT]

Hi David,

Welcome to ASPNET newsgroup.
As for dynamically creating UserControl on asp.net web page. In addition to
using Template DAtaBound control (as DataGrid, Repeater....), we can also
use Page.LoadControl to load control instance and add them into a certain
container control on the page. For example:

==================
private void Page_Load(object sender, System.EventArgs e)
{
..........
for(int i=0;i<1000;i++)
{
Control ctrl = Page.LoadControl("~/uc/MyUsercontrol.ascx");
ctrl.ID = "myuc_" +i;
MyPanel.Controls.Add(ctrl);
}
}


BTW, dynamically loading lots of usercontrols and adding into page's
control structure will cause performance issue especially when the
usercontrol is not simple one.

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "David Lozzi" <[email protected]>
| Subject: Create User Controls dynamically
| Date: Thu, 13 Oct 2005 01:37:16 -0400
| Lines: 16
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:5788
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| Howdy,
|
| I have a user control that needs to be displayed X amount of times on one
| page. How is this done? I was thinking of putting it into a datagrid,
which
| would work, but is there a better way?
|
| Thanks!
|
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
|
|
|
|
|
 
S

Steven Cheng[MSFT]

Hi David,

Have you got any further progress on this issue. or does the suggestions in
my last reply helps a little? If there're anything else we can help, please
feel free to post here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| X-Tomcat-ID: 178280068
| References: <#[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (e-mail address removed) (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Thu, 13 Oct 2005 09:55:44 GMT
| Subject: RE: Create User Controls dynamically
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| Lines: 53
| Path: TK2MSFTNGXA01.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:5789
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
| Hi David,
|
| Welcome to ASPNET newsgroup.
| As for dynamically creating UserControl on asp.net web page. In addition
to
| using Template DAtaBound control (as DataGrid, Repeater....), we can also
| use Page.LoadControl to load control instance and add them into a certain
| container control on the page. For example:
|
| ==================
| private void Page_Load(object sender, System.EventArgs e)
| {
| .........
| for(int i=0;i<1000;i++)
| {
| Control ctrl = Page.LoadControl("~/uc/MyUsercontrol.ascx");
| ctrl.ID = "myuc_" +i;
| MyPanel.Controls.Add(ctrl);
| }
| }
|
|
| BTW, dynamically loading lots of usercontrols and adding into page's
| control structure will cause performance issue especially when the
| usercontrol is not simple one.
|
| Hope helps. Thanks,
|
| Steven Cheng
| Microsoft Online Support
|
| Get Secure! www.microsoft.com/security
| (This posting is provided "AS IS", with no warranties, and confers no
| rights.)
|
| --------------------
| | From: "David Lozzi" <[email protected]>
| | Subject: Create User Controls dynamically
| | Date: Thu, 13 Oct 2005 01:37:16 -0400
| | Lines: 16
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| | X-RFC2646: Format=Flowed; Original
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| | Message-ID: <#[email protected]>
| | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| | Xref: TK2MSFTNGXA01.phx.gbl
| microsoft.public.dotnet.framework.aspnet.datagridcontrol:5788
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| |
| | Howdy,
| |
| | I have a user control that needs to be displayed X amount of times on
one
| | page. How is this done? I was thinking of putting it into a datagrid,
| which
| | would work, but is there a better way?
| |
| | Thanks!
| |
| | --
| | David Lozzi
| | Web Applications Developer
| | dlozzi@(remove-this)delphi-ts.com
| |
| |
| |
| |
| |
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top