Dynamically added user control throws object not set

Discussion in 'ASP .Net' started by andyrich_1@hotmail.com, Jan 3, 2006.

  1. Guest

    HI,

    I'm adding a usercontrol to a webform in the following way


    Dim ctlRate As New ctlRatesTable
    ctlRate.SomeMethod()
    Me.Page.Controls.Add(ctlRate)

    Where sub routine "SomeMethod" attempts to set the text property of a
    lable webcontrol. The problem is that the lable hasn't been created.
    I've tried calling EnsureChildControls() before using the lable control
    but this hasn't helped.

    Any ideas what I'm doing wrong?

    Thanks,
    Andy
     
    , Jan 3, 2006
    #1
    1. Advertisements

  2. Teemu Keiski Guest

    Try putting it also like this:

    ***
    Dim ctlRate As New ctlRatesTable

    ' FOLLOWING TWO LINES IN DIFFERENT ORDER THAN ORIGINALLY
    Me.Page.Controls.Add(ctlRate)
    ctlRate.SomeMethod()

    And yes, inside ctlRatesTable, before accessing child controls make sure
    EnsureChildControls is called (and child controls instantiated in
    CreateChildControls method)


    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    <> wrote in message
    news:...
    > HI,
    >
    > I'm adding a usercontrol to a webform in the following way
    >
    >
    > > > >

    > Where sub routine "SomeMethod" attempts to set the text property of a
    > lable webcontrol. The problem is that the lable hasn't been created.
    > I've tried calling EnsureChildControls() before using the lable control
    > but this hasn't helped.
    >
    > Any ideas what I'm doing wrong?
    >
    > Thanks,
    > Andy
    >
     
    Teemu Keiski, Jan 3, 2006
    #2
    1. Advertisements

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. Bennett Haselton
    Replies:
    1
    Views:
    734
    John M Deal
    Nov 8, 2004
  2. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    7,097
    Matt Berther
    Mar 5, 2005
  3. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,534
    Scott Allen
    Mar 4, 2005
  4. Jon B
    Replies:
    5
    Views:
    5,430
  5. =?Utf-8?B?U2FtdWVs?=
    Replies:
    7
    Views:
    1,216
    Scott Allen
    Aug 10, 2005
  6. Phoenix
    Replies:
    2
    Views:
    340
    Teemu Keiski
    Jun 6, 2005
  7. Cathy

    Dynamically Added User Control not accessible!

    Cathy, Dec 16, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    203
    Cathy
    Dec 16, 2003
  8. gishani ratnayake via .NET 247

    User Control Error : Object reference not set to an instance of an object.

    gishani ratnayake via .NET 247, Mar 11, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    254
Loading...