Controls Not Instantiated on UserControl

E

Eric Robishaw

What's the deal?

If I place a control, a standard Web control (like a panel, or server-side
Table), onto a Web User Control I've created, I can't access the control
from the Code-Behind code.

In the Page_Load event of the Web User Control, if I say:

PlaceHolder1.Controls.Add (new LiteralControl ("<img src='bla.gif'>"));

I get an error: Object null. And sure enough, PlaceHolder1 has no value
set.

If I do a "This.Controls.Count" I get a big fat zero.

What's the deal.

Desperate and annoyed.

Eric
 
A

Alessandro Zifiglio

Check to see if the control you added has the declration. Vs.net
automatically puts this declaration for you, unless you added this control
in html view and then switched to your code behind class.
 
F

Fraggle

Eric Robishaw said:
What's the deal?
<cut>
In the Page_Load event of the Web User Control, if I say:

PlaceHolder1.Controls.Add (new LiteralControl ("<img src='bla.gif'>"));

I get an error: Object null. And sure enough, PlaceHolder1 has no value
set.
<snip>
Eric

Try doing the action in page_init.

i have no idea why, but it works for me!

My lack of understanding of how asp.net works i expect!

fragg
 

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,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top