我有一個母版頁,這些線路中的負載事件:ASP.net C#墜毀在一行
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";
的VAR設定罰款,但CSS類線與錯誤崩潰:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
主網頁上有以下幾個要素:
<%@ 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">
<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>
任何想法?謝謝!我是.net的新手,這段代碼在它自己的頁面上工作得很好,我把它移到了母版頁上,並且我把縮小範圍縮小到了設置CSS類的那一行。
編輯
註釋掉行:
selectedMenu.CssClass = "navButtonO";
而且整個事情正常工作,因此它似乎只是此行失敗。
是否查詢字符串參數*有*除0,1,2或3之外的任何其他值?在任何訂閱此主人的內容頁面上? – 2010-08-10 15:11:15
那麼,這表明'selectedMenu'爲空 - 'menuID'的值是什麼? – 2010-08-10 15:11:29
目前我只是測試它的0,1,2,3的值,如果它不是一個值,它默認爲0.(int.tryParse) – 2010-08-10 15:18:35