2011-11-15 15 views
1

私の状況を理解するための簡単な例を参照してください。
(コメントに注意内部コード)コンテンツページからコントロール(マスターページ内)を検索すると、エラー= NullReferenceExceptionが発生する

マスターページ(ASPX):あなたは、私がClientIDModeに= "静的" を設定見るよう

<%@ 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> 

マスターページ(分離コード):

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 
     } 

    } 
} 

私はDataBindをSummaryContainerしたい(Aスパン)をコンテンツページのコードビハインドから削除します。 ので、私は以下のコードを試みた:

  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 ERROR持っている:

Object reference not set to an instance of an object.

エラーを修正し、そのスパンに新しい結果を表示するにはどうすればよいですか?

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

しかし、あなたの場合には、個人的に代わりにマスターページ上のすべてのことを置くことで、私はあなたを入れたい:私は、マスターページ上のインタフェースを使用したプロジェクトには、事前

答えて

1

感謝SummaryContainerをUserControlに追加し、ContentPlaceHolderを追加します。 Page_Loadメソッドはプロパティにアクセスすることができ、将来のページでは、その最初のPlaceHolderに別のUserControlを埋め込むことで、異なる要約情報を持つことができます。

また、愚かなエラーをデバッグすると、.Master.FindControlまたはlbl.TextにNull例外がスローされますか?

今は自分でデバッグすることはできませんが、それはpage life cycleが原因でしょうか。つまり、コンテンツページの読み込みはマスターページの読み込み前に行われますか?

+0

こんにちは、お返事ありがとうございます。 lbl.TextでNull例外がスローされています... – MoonLight

関連する問題