Professional Web Applications Themes

Dynamically loading user control into Placeholder gives Object reference not set to an instance of an object - ASP.NET Building Controls

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 ...

  1. #1

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

    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 Guest

  2. #2

    Default Re: Dynamically loading user control into Placeholder gives Object reference not set to an instance of an object

    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 Guest

  3. #3

    Default Re: Dynamically loading user control into Placeholder gives Object reference not set to an instance of an object

    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 Guest

Similar Threads

  1. Dynamically Loading User Control in PlaceHolder
    By Ugur Ekinci in forum ASP.NET Building Controls
    Replies: 2
    Last Post: November 27th, 04:37 PM
  2. Custom Control Problem :: Object reference not set to an instance of an object
    By David Bowey in forum ASP.NET Building Controls
    Replies: 1
    Last Post: March 16th, 10:33 PM
  3. Replies: 1
    Last Post: August 5th, 04:18 PM
  4. Replies: 0
    Last Post: July 25th, 10:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139