2011-11-15 64 views
1

請參閱下面的簡單示例以瞭解我的情況。
(注意要評論裏面代碼)從內容頁面查找控件(內部主頁面),錯誤= NullReferenceException

母版頁(ASPX):

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="NiceFileExplorer.Site1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <asp:ContentPlaceHolder ID="head" runat="server"> 
    </asp:ContentPlaceHolder> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <span runat="server" id="SummaryContainer"> 
      <asp:Label ID="lblDownload_Count_By_UserID_Today_Title" runat="server" Text="Count :" 
       ToolTip="Your Download Count-Today" CssClass="lblTitleInStatistics_Master"></asp:Label> 
      <asp:Label ID="lblDownload_Count_By_UserID_Today" runat="server" Text="<%# Download_Count_By_UserID_Today() %>" 
       CssClass="lblCountInStatistics_Master" ToolTip="Your Download Count-Today"></asp:Label> 
      <span style="color: white;">&nbsp;|&nbsp;</span> 
      <asp:Label ID="lblDownload_Size_By_UserID_Today_Title" runat="server" Text="Size :" 
       ToolTip="Your Download Size-Today" CssClass="lblTitleInStatistics_Master"></asp:Label> 
      <asp:Label ID="lblDownload_Size_By_UserID_Today" runat="server" Text="<%# Download_Size_By_UserID_Today() %>" 
       CssClass="lblCountInStatistics_Master" ToolTip="Your Download Size-Today"></asp:Label> 
     </span> 
    </div> 
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" ViewStateMode="Inherit" ClientIDMode="Static"> 
    </asp:ContentPlaceHolder> 
    </div> 
    </form> 
</body> 
</html> 

當你看到我設置的ClientIDMode = 「靜態」。

母版頁(代碼隱藏):

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace NiceFileExplorer 
{ 
    public partial class Site1 : System.Web.UI.MasterPage 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      SummaryContainer.DataBind(); 
     } 

     protected string Download_Count_By_UserID_Today() 
     { 
      //Read New Count From DataBase 
      //return Count; 
      return "Test"; 
     } 

     protected string Download_Size_By_UserID_Today() 
     { 
      //Read New Size From DataBase 
      //return Size; 
      return "Test"; 
     } 
    } 
} 

內容頁(ASPX):

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="NiceFileExplorer.WebForm1" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
Conntent Page 
</asp:Content> 

內容頁面(代碼隱藏):

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace NiceFileExplorer 
{ 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      MyMethod(); 
     } 

     private void MyMethod() 
     { 
      //Add New Downloaded File Info To DataBase(); -> For Getting Count And Size Of Them Per Day 

      //Here I Wand To Access Master Page Controls And Update Count And Size Lables 
      //So, I Tried Codes Below Without Any Results -> How Can I Fix This ? 
      var SummaryContainer = (System.Web.UI.HtmlControls.HtmlGenericControl)Page.Master.FindControl("SummaryContainer"); 
      SummaryContainer.DataBind(); 
      SummaryContainer.InnerHtml = "<h2>Hello World</h2>"; 

      //After Update Those Lables Failed, I test the codes Below With Null Execption Error -> How Can I Fix This ? 
      var lblDownload_Count_By_UserID_Today_Title = (Label)Page.Master.FindControl("lblDownload_Count_By_UserID_Today_Title"); 
      lblDownload_Count_By_UserID_Today_Title.Text = "test"; 

      DwonloadFile(); 
     } 

     private void DwonloadFile() 
     { 
      //A Class (Method) That Shows Download Window To My Users, So Page_Load Of Master Will Never Fire... 
      //And This Is The Reason That I want to update count & size lables from content page 
     } 

    } 
} 

我想數據綁定SummaryContainer(一從內容頁面的代碼隱藏。 所以我嘗試下面的代碼:

  var SummaryContainer = (System.Web.UI.HtmlControls.HtmlGenericControl)Page.Master.FindControl("SummaryContainer"); 
      SummaryContainer.DataBind(): 

,但我看不到新的成果。
那失敗後我試圖找到一個標籤的文本(即標籤內主站)從內容頁後面的代碼測試像下面的:var

lblDownload_Count_By_UserID_Today_Title = (Label)Page.Master.FindControl("lblDownload_Count_By_UserID_Today_Title"); 
        lblDownload_Count_By_UserID_Today_Title.Text = "test"; 

,但我有System.NullReferenceException錯誤:

Object reference not set to an instance of an object.

我該如何解決這個錯誤和強制跨度來顯示新結果?

在此先感謝

回答

1

在一個項目中我用了一個接口上的母版:

((IMasterPage)Page.Master).MyProperty = "test"; 

但在你的情況,親自而不是把所有的母版頁上,我把你的SummaryContainer進入UserControl,並有另一個ContentPlaceHolder。 然後,Page_Load方法將能夠訪問屬性,並且在未來頁面上,您可以通過用不同的UserControl填充第一個PlaceHolder來獲得不同的摘要信息。

還調試愚蠢的錯誤,是空的異常拋出在.Master.FindControllbl.Text

我現在無法爲自己調試它,但這是否歸因於page life cycle,即內容頁面加載在主頁面加載之前出現?

+0

嗨,謝謝你的回答。 Null異常正在拋出lbl.Text ... – MoonLight

相關問題