Add UserControl on CodeBeheind

K

Krzysztof Karnicki

Hi,
I've such a problem. I need to add my UserControl not on design time but on
Codebehind, so I prepare:

private void Page_Load(object sender, System.EventArgs e) {
MyUserControl myUserControl = new MyUserControl();
this.Controls.Add(myUserControls);
}

But actually that doesn’t work. After requesting my page on a browser, there
aren’t shown any controls putted on myUserControl. Also it is thrown an
exception of null reference if I tried to modify anything in codebehind of
components putted in myUserControl, when he is added in such a way. Any
suggestions ?


Regards
Krzysztof Karnicki
 
K

Krzysztof Karnicki

Hi,

I'm adding myUserControl, wich is the instance of class inhriting from the
System.Web.UI.UserControl.
 
W

Wilbur Slice

Hi,
I've such a problem. I need to add my UserControl not on design time but on
Codebehind, so I prepare:

private void Page_Load(object sender, System.EventArgs e) {
MyUserControl myUserControl = new MyUserControl();
this.Controls.Add(myUserControls);
}

But actually that doesn’t work. After requesting my page on a browser, there
aren’t shown any controls putted on myUserControl. Also it is thrown an
exception of null reference if I tried to modify anything in codebehind of
components putted in myUserControl, when he is added in such a way. Any
suggestions ?


Regards
Krzysztof Karnicki


Try putting a PlaceHolder control on your form at design time, and
then Add your control to it, rather than adding it to the page's
Controls collection.
 
K

Krzysztof Karnicki

Hi,
When I try implement Yous sugestion:

WebUserControl1 webUserControl = new WebUserControl1();
this.PlaceHolder1.Controls.Add(webUserControl);

he same situation occurs. Because webUserControl try to change Text property
of Label putted on my control, requesting page throws an null excpetion.

Regards,
Krzysztof Karnicki
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top