2015-11-21 292 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

相關問題