2015-11-21 186 views
0

我正在开发ASP.NET应用程序,其中代码在本地IIS上适用于桌面和移动设备均正常工作。 但是,当我将代码复制到生产服务器时,它在桌面上运行良好,但在移动设备上运行良好。过去几天我一直在努力解决这个问题,但仍然没有成功。在桌面上工作的代码但在移动设备上不工作

这是错误:

[NullReferenceException: Object reference not set to an instance of an object.] BasePage.OnLoad(EventArgs e) +368
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

这是我的基本页面代码,它似乎有问题:

HtmlGenericControl liItem1 = new HtmlGenericControl(); 

liItem1 = (HtmlGenericControl)this.Master.FindControl("logtop_bar"); 

liItem1.Attributes.Add("style", "display:block"); 

我不明白为什么它抛出错误只为小逻辑屏幕移动设备。

但同样不会在本地环境中重现。

+0

“FindControl”实际上是否在移动设备上找到了logtop_bar?如果您进入调试器(使用开发工具将Chrome移动到移动模式),您是否遇到问题?或者,您始终可以将文本输出到屏幕以查看它是什么。 –

+0

我在chrome中安装了一些插件。这很容易洗牌黑白移动设备和桌面view.It似乎当我切换到移动视图。 – KapilS

+0

FindControl抛出null,虽然不知道为什么.. – KapilS

回答

0

建立在[Ondrej Svejdar]的答案... 在您的解决方案中,可能存在Site.Master和Site.Mobile.Master。即使您期待,选择其中一种的机制也可能不会选择Mobile版本。试试这个

// the assignment below is not needed since you are assigning it again on the very next line 
//HtmlGenericControl liItem1 = new HtmlGenericControl(); 
HtmlGenericControl liItem1 = (HtmlGenericControl)this.Master.FindControl("logtop_bar"); 
// this line below will print which master page you are using 
Response.Write(this.Master.MasterPageFile); 
// only set the attribute if it is not null. 
if (liItem1 != null) { 
    liItem1.Attributes.Add("style", "display:block"); 
} 

此外,如果你总是设置样式(我没有看到任何条件),那么就在页面的标记中这样做。

+0

好点评总是空,是的两个文件都在那里。我最终通过在web.config中编写规则避免了这种情况。这对我来说根本不起作用。 – KapilS

+0

如果liItem始终为空,那么它在母版页上不存在。 – Phil

+0

它并不总是为空,仅适用于移动页面。 – KapilS

相关问题