Properpies and Controls on a Dynamically Instanciated UserControl

Discussion in 'ASP .Net' started by Andrew Robinson, Aug 3, 2006.

  1. I have a user control that I am dynamically loading at run time:

    PersonalInformationUserControl pi = new PersonalInformationUserControl();
    PlaceHolderPersonalInformation.Controls.Add(pi);

    My user control has public properties that in turn access controls on the
    UC. I can access the properties, but the subsequent access to the underlying
    controls generates a null reference exception. How do I get the control to
    build / load all of its content?


    pi.DisplayOnly = true; // ok
    pi.Email = ""; // null ref on the TextBoxEmail control


    within the PI control:


    public string Email
    {
    get { return TextBoxEmail.Text; }
    set { TextBoxEmail.Text = value; }
    }


    Thanks,
     
    Andrew Robinson, Aug 3, 2006
    #1
    1. Advertising

  2. Hello Andrew,

    As for ascx UserControl, it is different from custom webserver control, you
    should use the "LoadControl" method(of TemplateControl or Page class) to
    create the UserControl instead of simply calling the constructor. For
    example:

    ================
    Control control = Page.LoadControl("~/usercontrols/myusercontrol.ascx");

    Panel1.Controls.Add(control);
    ================

    Here are some reference articles mentioned this:

    #how to dynamically add user controls to pages at runtime using the
    LoadControl method.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
    frlrfSystemWebUITemplateControlClassLoadControlTopic.asp

    http://aspalliance.com/565


    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Aug 4, 2006
    #2
    1. Advertising

  3. Andrew Robinson

    Matt Sollars Guest

    Hi Andrew,

    Your properties' accessors need to call one method before accessing
    child controls in order to ensure that they have been created.

    public string Email
    {
    get {
    EnsureChildControls();
    return TextBoxEmail.Text;
    }
    set {
    EnsureChildControls();
    TextBoxEmail.Text = value;
    }
    }

    Good luck!

    - Matt


    ----- Original Message -----
    From: Andrew Robinson
    Date: 8/3/2006 1:22 PM
    > I have a user control that I am dynamically loading at run time:
    >
    > PersonalInformationUserControl pi = new PersonalInformationUserControl();
    > PlaceHolderPersonalInformation.Controls.Add(pi);
    >
    > My user control has public properties that in turn access controls on the
    > UC. I can access the properties, but the subsequent access to the underlying
    > controls generates a null reference exception. How do I get the control to
    > build / load all of its content?
    >
    >
    > pi.DisplayOnly = true; // ok
    > pi.Email = ""; // null ref on the TextBoxEmail control
    >
    >
    > within the PI control:
    >
    >
    > public string Email
    > {
    > get { return TextBoxEmail.Text; }
    > set { TextBoxEmail.Text = value; }
    > }
    >
    >
    > Thanks,
    >
    >
     
    Matt Sollars, Aug 6, 2006
    #3
  4. Hi Andrew,

    How are you doing on this issue, does my last reply helps you on this issue?

    Please let me know if you meet any further problems on this.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Aug 8, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. huobazi
    Replies:
    1
    Views:
    743
    David Waz...
    Jul 3, 2003
  2. Rajesh Tiwari

    Can we use a usercontrol inside a usercontrol

    Rajesh Tiwari, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    444
    Rajesh Tiwari
    Aug 12, 2003
  3. ThunderMusic
    Replies:
    4
    Views:
    437
    ThunderMusic
    Dec 21, 2005
  4. Sylvain Audi
    Replies:
    1
    Views:
    283
    Ian Collins
    Jan 13, 2007
  5. nitin

    Event not firing. Adding controls dynamically to UserControl

    nitin, Jul 16, 2003, in forum: ASP .Net Building Controls
    Replies:
    5
    Views:
    193
    Patrick Sullivan
    Jul 23, 2003
Loading...

Share This Page