Avoiding a NullReferenceException on MasterPage

I went in to add a page to a site that's been alive for about 11 months. I wanted to add a page in a subfolder to test out some user/role setup on the subfolder, and instead have been debugging a NullReferenceException.

When my MasterPage.master.cs Page_Load ran, I found the root of the problem was all my controls appeared to be null, which was confusing since 'this' was live.

Bottom-line:

When I added my new page, I took the default given to me by Visual Studio after selecting my MasterPage.master. This page has an assortment of contentplaceholders on it, all but one of which is now handled by my MasterPage code. Since the page was irrelevant to the role-testing I was doing, I just accepted the default and tried running.

The fact is, I should have removed all the contentplaceholders that the MasterPage code was handling, because when it tried to execute the code-behind, it was trying to execute it on the content in the placeholders on my new page which were empty, so of course the controls were going to be null.

Removing the contentplaceholders on the new page allowed the MasterPage to do it's thing, and now my page is fine.

posted @ Wednesday, November 22, 2006 10:17 AM

Print

Comments on this entry:

No comments posted yet.

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345