Site.Master中标签上的空引用异常

时间:2013-04-10 17:44:40

标签: asp.net c#-4.0

对于我的生活,我无法弄清楚为什么我在我的Site.Master页面上的标签上获得Null Reference Exception。我在同一页面上有2个其他标签,这些标签不会抛出错误。

以下是包含所有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 Label是抛出错误的新标签。

以下是我的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();
       }
}

“if(iOSLabel!= null)”这一行是错误。

正如其他地方所建议的,我尝试了注明here的代码,并添加了:

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

并相应地更改了CS代码。

当我测试它时,新的iOSLabel现在抛出了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)

1 个答案:

答案 0 :(得分:0)

而不是

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

尝试

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