views:

17

answers:

3

Hi All

I've got a custom ASP.Net web user control I've built; done this enough times before but this one is misbehaving and I can't spot why.

Where the calling page includes the user control directly in its markup, all is well and the control behaves as expected.

However if the page adds this particular control dynamically (to a placeholder in the master page, which is what's calling this whole thing) the elements within it stay firmly NULL - nothing from the user control gets written to the client at all, including static content within the user control.

Where might I be going wrong?

A: 

When you add user control dynamicly you have to generate uniqe ID for each added control.

For example:

Control selWebControl = (Control)Page.LoadControl("~/DL/Templates/FileLibrary.ascx");
selWebControl.ID = "UC" + "_" + dfRow.ID;
Siekiera
I don't have to generate an ID - I was fairly sure the auto-generated default was good enough and I've confirmed that makes no difference. You've reminded me though that I need to use LoadControl, and _that_ solves the problem :-)
eftpotrm
A: 

Make sure you are adding it to a Controls collection that is on the page and make sure you are doing it at the right time in the Page LifeCycle. I like to override CreateChildControls.

So make sure you are doing Page.Controls.Add(myNewControl) or PlaceHolder1.Controls.Add(myNewControl).

Might help if you showed the code in which you are dynamically creating the control and adding it to the page

matt-dot-net
A: 

Answer sorted - IDs are insignificant, it can do without, but if you do

Control a = new Control;

it doesn't work whereas

Control a = (Control)Page.LoadControl("~Folder/Control.ascx");

Does, which is what I'd forgotten late at night :-)

eftpotrm