对于我的一生,我无法弄清楚为什么我在 Site.Master 页面中的标签上出现空引用异常。我在同一页面上有 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 标签是引发错误的新标签。
这是我的 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)”这一行是错误。
正如其他地方所建议的,我尝试了这里提到的代码,并添加了:
Label iOSLabel = (Label)Master.FindControl("lblOS");
并相应地更改了 CS 代码。
当我测试它时,那个新的 iOSLabel 现在正在抛出空异常。
有关如何解决此问题的任何想法?
编辑: 这是生成的错误...
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)