Add Web User Control programmatically RRS feed

  • Question

  • User1872335707 posted

    Hi all,

    I have an web user control that want to add some another user control to it programmatically. For this:

    //Load event of parent user control:


    PictureBannerControl c = new PictureBannerControl();
            c.Banner = Banner.GetSomeRandomBanner();

     If I add this control (PictureBannerControl) in design mode to web page, this work correctly.

    I have this in Load of PictureBannerControl:

    if (banner != null)
                    link.HRef = banner.LinkUrl;
                    Image1.ImageUrl = string.Format("~/Banners/{0}.{1}", banner.ID.ToString(), banner.PicPath);
                    Image1.ToolTip = banner.Text;
                    Label1.Text = banner.Title;

     that Banner is a property in PictureBannerControl.

    But if I add this programmatically like first example code, When the first line in load run, an exception thrown and link value (a hyper link control) is null.

    What do I do for add dynamicly this user control? (note: controls in PictureBannerControl must be set by Banner value)


    Saturday, August 30, 2008 6:54 AM

All replies

  • User2130758966 posted

     Where are the controls link, Image1 and Label1 coming from?

    Asp.net cant find them so you need to tweak this out and it will work!

    Sunday, August 31, 2008 6:57 AM
  • User1872335707 posted

    These controls are in my user control. (child user control).

    Sunday, August 31, 2008 9:10 AM
  • User481221548 posted

    Hi there

    Problem already solved?
    If not, try to reproduce the problem with a new UserControl.

    How does the designer.cs file looks like?
    What means the Debugger, where and when the ChildControls are null or not?

    Sunday, August 31, 2008 11:23 PM