1

私の状況を理解するには、以下の簡単な例を参照してください。
(コード内のコメントへの注意)

マスターページ(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="Static"を設定しました。

マスターページ(CodeBehind):

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>

コンテンツページ(CodeBehind):

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
        }

    }
}

コンテンツページのコードビハインドからDataBindSummaryContainer(スパン)を作成したい。だから私は以下のコードを試しました:

            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.

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

前もって感謝します

4

1 に答える 1

2

プロジェクトでは、マスターページのインターフェイスを使用しました。

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

しかし、あなたの場合、個人的には、すべてをマスターページに配置する代わりに、あなたSummaryContainerをUserControlに配置し、別のを使用しContentPlaceHolderます。その後、Page_Loadメソッドはプロパティにアクセスできるようになり、将来のページでは、最初のPlaceHolderに別のUserControlを入力することで、別の要約情報を取得できます。

また、愚かなエラーのデバッグでは、Null例外が.Master.FindControlまたはでスローされていlbl.Textますか?

現在、自分でデバッグすることはできませんが、ページのライフサイクル、つまりコンテンツページの読み込みがマスターページの読み込みよりも前に行われることが原因でしょうか。

于 2011-11-15T06:51:04.153 に答える