2013-04-10 19 views
2

在標籤上的空引用異常對於我的生活,我無法弄清楚爲什麼我會在我的Site.Master頁面中找到標籤上的空引用異常。我在同一頁面上有兩個其他標籤,這些標籤不會引發錯誤。Site.Master

這裏是所有3個標籤的DIV:

<div class="footer"> 
    <asp:Label ID="lblOS" runat="server" /> 
    <asp:Label ID="lblFooter" runat="server" /><br /> 
    <asp:Label ID="lblFooterEx" runat="server" Text="<% $Resources:mobileResource,FooterExtra %>" /> 
</div> 

的lblOS標籤是新的引發錯誤。

這裏是我的Site.Master.cs文件中的代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 
     //Load App Store Icon 
     string strUserAgent = Request.UserAgent.ToString().ToLower(); 
     if (lblOS.Text != null) 
     { 
      if (strUserAgent.Contains("iphone") || strUserAgent.Contains("ipad")){ 
       lblOS.Text = "<a href='https://itunes.apple.com/'><img src='Images/appStore.jpg' class='connectIcons' title='App Store' alt='App Store' /></a><br />"; 
      } 
      else{ 
       lblOS.Text = null; 
      } 
     } 
     //Load Footer Links 
     string url = Request.ServerVariables["URL"]; 
     url = url.Remove(0, url.LastIndexOf("/") + 1); 

     if (url == "Default.aspx") { 
      lblFooter.Text = GetGlobalResourceObject("mobileResource", "FooterNav").ToString(); 
     } 
     else { 
      lblFooter.Text = GetGlobalResourceObject("mobileResource", "FooterHome").ToString() + GetGlobalResourceObject("mobileResource", "FooterNav").ToString(); 
     } 
} 

行 「如果(!iOSLabel = NULL)」 是錯誤。

與其他地方一樣的建議,我試過代碼指出here,並補充說:

Label iOSLabel = (Label)Master.FindControl("lblOS"); 

,並相應地改變了CS代碼。

當我測試它時,新的iOS標籤現在拋出null異常。

有關如何解決此問題的任何想法?

編輯: 下面是系統產生的錯誤...

Error: System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object. 
    at CLVmobileApp.SiteMaster.Page_Load(Object sender, EventArgs e) in C:\Visual Studio\AS-MobileCLV\CLVmobileApp\Site.Master.cs:line 15 
    at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) 
    at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) 
    at System.Web.UI.Control.OnLoad(EventArgs e) 
    at System.Web.UI.Control.LoadRecursive() 
    at System.Web.UI.Control.LoadRecursive() 
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
    at System.Web.UI.Page.HandleError(Exception e) 
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
    at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
    at System.Web.UI.Page.ProcessRequest() 
    at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) 
    at System.Web.UI.Page.ProcessRequest(HttpContext context) 
    at ASP.default_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\cbd2add4\2961a9ef\App_Web_ddrybdwm.17.cs:line 0 
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 
+0

你在哪裏訪問該標籤即。在MasterPage本身或內容頁面上? – Sachin 2013-04-10 17:52:48

+0

帶我的標籤的DIV只在我的Site.Master頁面上。 (這是我的網站的頁腳。) – Psykopup 2013-04-10 17:55:50

+0

我沒有看到你的示例代碼中的錯誤行。 – Floremin 2013-04-10 18:32:38

回答

0

而不是

Label iOSLabel = (Label)Master.FindControl("lblOS"); 

嘗試

Label iOSLabel = (Label)(Master as MyMasterPage) .FindControl("lblOS");