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

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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.

.



Relevant Pages

  • Re: Insert Server Control Into HTML String (via Literal) at runtime
    ... >> another user control in the middle of that HTML. ... The final result is that they get a content rotator ... >>> James Doughty ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Insert Server Control Into HTML String (via Literal) at runtime
    ... > another user control in the middle of that HTML. ... The final result is that they get a content rotator ... > The user creates the above complete HTML string - which gets saved to ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Insert Server Control Into HTML String (via Literal) at runtime
    ... What IS getting split is some HTML text (supplied by the ... The final result is that they get a content rotator in the middle ... contained within the user control that I'm injecting into the middle of the ... to display their content elsewhere in the app - I pull that HTML string out ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: ID & NaimgContainer
    ... let's make it clearer (hopefully) ... My HTML looks like that: ... the code for the user control which output this part of the page looks like ... protected override void CreateChildControls() ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: sending HTML email
    ... As far as using a user control for what you're doing, ... If you want to maintain the html externally, ... > Thanks, Justin. ... >> public function GetMyText as string ...
    (microsoft.public.dotnet.framework.aspnet)