Question

Please See The Simple Example Below For Understanding My situation.
(Attention To Comments Inside Codes)

Master Page (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>

as you see i set ClientIDMode="Static".

Master Page (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";
        }
    }
}

Content Page (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>

Content Page (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
        }

    }
}

i want to DataBind SummaryContainer(a span) from content page's code-behind. so i tried the codes below :

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

but i can not see new results.
After That Fail I tried to find a label's text(that label is inside Master) from content page code behind for test like below : 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"; 

but i have System.NullReferenceException ERROR :

Object reference not set to an instance of an object.

how can i fix that error and force that span to show me new results?

thanks in advance

Was it helpful?

Solution

In a project I used an interface on the masterpage:

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

But in your case, personally instead of putting all that on the master page, I'd put your SummaryContainer into a UserControl, and have another ContentPlaceHolder. Then the Page_Load method will be able to access the properties, and on future pages you could have different summary info by filling that first PlaceHolder with a different UserControl.

Also debugging stupid errors, is the Null exception being thrown at .Master.FindControl or at lbl.Text?

I'm unable to debug it for myself right now, but would it be due to the page life cycle, namely that Content Page Load comes before Master Page Load?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top