Sign in to follow this  
Cybrosys

[.net] Dynamically adding WebUserControls problems

Recommended Posts

Cybrosys    186
I created a WebUserControl which i named NewsControl. This control consists of 2 panels, 2 labels and 1 text box. When i add the control manually to any page using <news:NewsControl ID="NewsControl1" runat="server" /> It displays and works just fine, the problem is i want to add them dynamically. I added a <asp:Table ...>..</asp:Table> to a page and in that page's Page_Load function i wrote the simple lines: List<News> news = NewsController.GetNews(); int count = 0; foreach (News entry in news) { TableRow row = new TableRow(); TableCell cell = new TableCell(); NewsControl newsControl = new NewsControl(); newsControl.ID = string.Format("NewsControl{0}", count++); cell.Controls.Add(newsControl); row.Cells.Add(cell); TableNews.Rows.Add(row); } No major complicated problems with that code, except that the NewsControl objects doesn't get drawn when i view the page! That's right, when viewing the page source code the table is empty! The code gets called without problems. If i changed my NewsControl to a simple Button instead, it gets drawn. So apparently there's something seriously wrong with asp.net, any ideas on how to get this to work?

Share this post


Link to post
Share on other sites
mstrobel    172
Quote:
Original post by Cybrosys
...
NewsControl newsControl = new NewsControl();
newsControl.ID = string.Format("NewsControl{0}", count++);
cell.Controls.Add(newsControl);
...

Try this:
...
NewsControl newsControl = Page.LoadControl("NewsControl.ascx") as NewsControl;
newsControl.ID = string.Format("NewsControl{0}", count++);
cell.Controls.Add(newsControl);
...


You may have to change the filename if you're not using "NewsControl.ascx".

Share this post


Link to post
Share on other sites
Cybrosys    186
Thank you for the reply!

I will test what you're proposing, hopefully it will work. I seem to remember seeing other ways than my own way, to instantiate custom user controls. Though i fail to see why my original code doesn't work. It's as if it's only instantiating the Code-Behind part of the object and completely ignoring the asp.net part somehow. This might be because, opposed to for example the Button object, mine is a collection of controls. Maybe i just need to define a custom constructor for my control and somehow force the instantiation of the asp.net content... but I'm just rambling on, thank you for the help and I'll post whether or not it worked.

Thank you.

Share this post


Link to post
Share on other sites
mstrobel    172
I think you basically hit the nail on the head as far as the reason your way wasn't working. The link between the ASCX content page and the code-behind class is defined within the ASCX file--not the other way around. So if you add a user control by just invoking its constructor, then the ASP.NET engine can't load the actual control content defined in the ASCX file. But if you use Page.LoadControl(...) then it will pull the content from the ASCX file and resolve the control class from the <%@ Control ... %> tag.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this