Home All Groups Group Topic Archive Search About
Author
13 Jul 2005 9:26 AM
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

Author
13 Jul 2005 5:22 PM
Jason L Lind
are you adding myUserControls or myUserControl?

Show quoteHide quote
"Krzysztof Karnicki" wrote:

> 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
>
Are all your drivers up to date? click for free checkup

Author
13 Jul 2005 6:55 PM
Krzysztof Karnicki
Hi,

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


Show quoteHide quote
"Jason L Lind" wrote:

> are you adding myUserControls or myUserControl?
>
> "Krzysztof Karnicki" wrote:
>
> > 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
> >
Author
13 Jul 2005 9:31 PM
Wilbur Slice
On Wed, 13 Jul 2005 02:26:01 -0700, Krzysztof Karnicki
<KrzysztofKarni***@discussions.microsoft.com> wrote:

Show quoteHide quote
>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.
Author
13 Jul 2005 9:41 PM
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


Show quoteHide quote
"Wilbur Slice" wrote:

> On Wed, 13 Jul 2005 02:26:01 -0700, Krzysztof Karnicki
> <KrzysztofKarni***@discussions.microsoft.com> wrote:
>
> >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.
>
>
>
>

Bookmark and Share

Post Thread options