MasterPages & UserControls - object reference not set

J

j-in-uk

I have 3 pages Site.master, Album.aspx and ContinentsMenu.ascx.
Continents is a usercontrol placed in Albums. In Album.aspx.cs I need
to access a property from the Continents usercontrol and is done using
the code below. When I add masterpages to Album.aspx and remove all the
html tags I get the error System.NullReferenceException: Object
reference not set to an instance of an object. for the highlighted
yellow line below. I notice once I insert the following line
back<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> back
to the Album.aspx page and remove the masterpage references it works
fine. Any ideas on how i can access my usercontrol using masterpages?

private ContinentsMenu _continentList;

override protected void OnInit(System.EventArgs e)
{base.OnInit(e);
InitializeComponent();
_continentList = ((ContinentsMenu)(FindControl("ContinentsMenu1")));
}

protected void Page_Load(object sender, System.EventArgs e)
{_albumPath = Path.Combine(_photosPath,
_continentList.SelectedContinent);}
 
D

Dave Sexton

Hi,

Your .aspx markup is probably invalid but it's hard to say without seeing it. To see valid markup for an implementation of the
content of a master page (assuming your using VS.NET 2005), add a new web page to your project but before continuing check the
"choose a master page" checkbox. After you close the "New File" dialog another opens. Select Site.master from the list and
continue.

Look at the content of the new page to see valid markup.
 
J

j-in-uk

Hi Dave,

Yes that's what I did when I added my Album.aspx page I selected
Site.master option, for all my other pages it works fine. Just my
album.apx page which has a usercontrol, it throws a
System.NullReferenceException error on the following line for
_continent.SelectedContinent

protected void Page_Load(object sender, System.EventArgs e)
{_albumPath = Path.Combine(_photosPath,
_continentList.SelectedContinent);}
 
D

Dave Sexton

Hi,

Try using the following in the OnInit event handler:

phMenu.Controls.Add(Page.LoadControl("~/ContinentsMenu.ascx"))

where phMenu is declared in the .aspx page where you want your UserControl to be loaded:

<asp:placeHolder runat="server" ID="phMenu" />

HTH
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top