繁体   English   中英

从内容页面中查找控件(在母版页内部),错误= NullReferenceException

[英]Find A Control (Inside Master Page) From Content Page, Error = 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 =“ 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>

内容页(代码隐藏):

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(一个跨度)。 所以我尝试了以下代码:

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

但我看不到新结果。
失败之后,我尝试从后面的内容页面代码中查找标签的文本(该标签在Master内),如下所示: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.

如何解决该错误并强制跨度向我显示新结果?

提前致谢

在一个项目中,我在母版页上使用了一个界面:

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

但就您而言,我个人不是将所有内容放到母版页上,而是将您的SummaryContainer放到UserControl中,并使用另一个ContentPlaceHolder 然后,Page_Load方法将能够访问属性,在以后的页面上,可以通过使用不同的UserControl填充第一个PlaceHolder来获得不同的摘要信息。

还调试愚蠢的错误,是在.Master.FindControl还是在lbl.Text抛出Null异常?

我现在无法自己调试它,但这是否归因于页面生命周期 ,即内容页面加载早于母版页面加载?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM