Dynamically loading user control into Placeholder gives Object reference not set to an instance of a

Discussion in 'ASP .Net Building Controls' started by Phoenix, Jun 6, 2005.

  1. Phoenix

    Phoenix Guest

    I've created user controls that contain listboxes that are dynamically
    populated from the database. In the html view of the user control
    (ucSectorsIndustries.ascx) is the <asp:listbox id="lbSector"
    runat="server" Rows="5"></asp:listbox> code. In the codebehind I have
    the following:

    Protected WithEvents lbSector As System.Web.UI.WebControls.ListBox
    ....
    On Page_Load of the user control I have

    For i As Integer = 0 To 5
    lbIndustry.Items.Add(New ListItem("----", ""))
    Next
    lbIndustry.DataBind()

    This usercontrol works great in a page on which it is dropped via the
    GUI, but on another page where it is loaded like this:
    Dim mySectorsIndustries As ucSectorsIndustries = New
    ucSectorsIndustries
    PlaceHolder1.Controls.Add(mySectorsIndustries)

    it seems to be NULL and gives the old "
    Exception Details: System.NullReferenceException: Object reference not
    set to an instance of an object."

    Source Error: (Line 69 is Red)

    Line 68: For i As Integer = 0 To 5
    Line 69: lbIndustry.Items.Add(New ListItem("----", ""))
    Line 70: Next
    Line 71: lbIndustry.DataBind()


    I have a feeling it is the way it is instantiating the object, but I
    don't know how to fix it...It is as if the HTML part of the user
    control is loading AFTER the Page_Load in the codebehind. Please help.
    Phoenix, Jun 6, 2005
    #1
    1. Advertising

  2. Phoenix

    Phoenix Guest

    I found the problem, i wasn't calling
    Page.LoadControL("ucMySectorFilters.ascx") before adding the
    control...duh.

    Here's the working code (hopefully it will help out someone else!):

    Dim mySectorFiltersControl As ucMySectorFilters = New ucMySectorFilters
    mySectorFiltersControl =
    Page.LoadControl("ucMySectorFilters.ascx")
    mySectorFiltersControl.listBoxStyle =
    ucMySectorFilters.enlistBoxStyle.ContentPage
    PlaceHolder1.Controls.Add(mySectorFiltersControl)
    Phoenix, Jun 6, 2005
    #2
    1. Advertising

  3. Phoenix

    Teemu Keiski Guest

    In your original code it seemed as if the code

    lbIndustry.Items.Add(New ListItem("----", ""))

    tries to refer to lbIndustry which wouldn't exist as you have lbSector in
    the control.

    And yes as you noticed, indeed as you instantiate the class it instantiates
    only the code-behind part of the UC; which has no knowledge of the markup.
    Therefore you need to use LoadControl.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    Teemu Keiski, Jun 6, 2005
    #3
    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. Pedro Correia
    Replies:
    0
    Views:
    624
    Pedro Correia
    Jul 25, 2003
  2. Greg
    Replies:
    0
    Views:
    897
  3. Replies:
    1
    Views:
    477
    Mark Rae [MVP]
    Sep 20, 2007
  4. Object Delayed loading dynamically into Placeholder

    , Oct 5, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    125
  5. 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:
    170
Loading...

Share This Page