tags:

views:

65

answers:

4

hi all,

I have a master page, with these lines in the on load event:

    string menuIDdata = Page.Request.QueryString["mid"];
    menuID = 0;

    // Get the menu ID
    if (!int.TryParse(menuIDdata, out menuID))
    {
        menuID = 0;
    }

    // Select the correct menu
    var selectedMenu = this.Page.FindControl("mnu" + menuID) as Panel;
    selectedMenu.CssClass = "navButtonO";

The var is set fine, but the CSS class line crashes with the error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

The master page has these elements on it:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="AdminMaster.master.cs" Inherits="MySite.MasterPages.AdminMaster" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 
    1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="mainHead" runat="server" >
        <title>Administration</title>
        <link rel="Stylesheet" href="../style/admin.css" />       
    </head>
    <body>

    <form id="mainForm" runat="server">

    <div class="topMenu">    

        <asp:Panel id="mnu0" runat="server" CssClass="navButton">
            <a href="admin.aspx?mid=0" class="navLink">Admin Home</a>
        </asp:Panel>

        <asp:Panel id="mnu1" runat="server" CssClass="navButton">
            <a href="admin.aspx?mid=1" class="navLink">User Manager</a>
        </asp:Panel>

        <asp:Panel id="mnu2" runat="server" CssClass="navButton">
            <a href="admin.aspx?mid=2" class="navLink">Products</a>
        </asp:Panel>  

        <asp:Panel id="mnu3" runat="server" CssClass="navButtonR">
            <a href="../default.aspx" class="navLink">Back to Site</a>
        </asp:Panel>
    </div>

    <br /><br />
    <asp:Panel id="subLinks" runat="server" CssClass="subMenu"></asp:Panel>

    <div class="mainContent">
        <asp:contentplaceholder id="mainContent" runat="server" />
    </div>



    </form>
    </body>
</html>

Any ideas? Thanks! I'm new to .net, this code worked fine when it was on it's own page, I've moved it to a master page and I have narrowed the crash down to the line where it sets CSS class.

Edit

Commenting out the line:

selectedMenu.CssClass = "navButtonO";

And the whole thing works fine, so it appears just this line is failing.

+4  A: 
var selectedMenu = this.Page.FindControl("mnu" + menuID) as Panel;

The above line is either failing to find the control on the page that matches the ID you're passing or it is failing to cast the control that is found to a Panel.

Set a breakpoint on that line and make sure that there is a Control on the Page that matches the ID you think should be there.

Justin Niessner
+1  A: 

I think you're missing condition where you don't have anything in your querystring. You should check it first:

string menuIDdata = Page.Request.QueryString["mid"];
if (String.IsNullOrEmpty(menuIDdata))
    return;

Check if it's null or empty and do not render your submenu.

Edit:

ok i see that you set 0, when queryString is empty, maby you're searching masterpage in wrong way, check this out: http://www.west-wind.com/Weblog/posts/5127.aspx and try this method:

/// <summary>
/// Finds a Control recursively. Note finds the first match and exists
/// </summary>
/// <param name="ContainerCtl"></param>
/// <param name="IdToFind"></param>
/// <returns></returns>
public static Control FindControlRecursive(Control Root, string Id)
{
    if (Root.ID == Id)
        return Root;

    foreach (Control Ctl in Root.Controls)
    {
        Control FoundCtl = FindControlRecursive(Ctl, Id);
        if (FoundCtl != null)
            return FoundCtl;
    }

    return null;
}

usage:

var selectedMenu = FindControlRecursive(this.Master, "mnu" + menuID) as Panel; 
Jarek
Thanks for your time, it throws the same error unfortunatly :-(
Tom Gullen
A: 

Your Panel control will most likely be nested in the pages Form control which will be a child of the Page control.
To get this to work you could create some kind of recursive method to locate the control you are after and only then check to see if you have located it before attempting to assign any property values to it.

Andy Rose
It just stopped working on my master page, on it's own page it worked 100% fine, is this the wrong way to change an elements CSS class?
Tom Gullen
The method for assigning the css class is fine, the exception suggests you are trying to set the CssClass of a null object. I'm surprised Jarek's recursive method hasn't worked out for you, that should retrieve the object you are looking for!
Andy Rose
A: 

You are searching in the wrong place for the panel. The master page is not part of the page. If you change

var selectedMenu = this.Page.FindControl("mnu" + menuID) as Panel;

to

var selectedMenu = this.Master.FindControl("mnu" + menuID) as Panel;

You might have more luck.

PS I can't remember when the master property was added to the Page class. You don't say what version you are running. So this code may not apply, but the concept is sound.

Philip Smith
Thanks for your time but same error applies :-S
Tom Gullen