Search code examples
c#asp.netmaster-pages

How to make Master Page to display a value on its label on each page


I am designing a Change Request(CR) module for our website, which allows users to raise a CR and submit it for review. A workflow gets generated immediately after raising CR, so, user have to submit it by voting to his activity(Say activity as 'Submit to CCB'). Then I am setting a label's value which is added to master page as 'In Review' I can see label value now, and immediately navigating to next activity(next page). But I could not see the label's value there in next page. As I am new to implementing master page concept, unable to find out the reason.

WFLCRMaster.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="WFLCR.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="masterFormMIF" runat="server">
  <div id="WorkflowStatus">
   <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager> 
              <asp:UpdatePanel ID="UserUpdatePanel" runat="server">
                  <ContentTemplate>
                  <asp:Label ID="WorkflowSignoffStatus" runat ="server">            </asp:Label>
                  </ContentTemplate>
              </asp:UpdatePanel>
          </div>
          <div>
           <asp:ContentPlaceHolder id="ContentPlaceHolderMIF" runat="server">
           </asp:ContentPlaceHolder>
         </div>
    </form>
  </body>

And I created a property in WFLCR.master.cs, And added <%@ MasterType VirtualPath="~/WFLCR.master" %> to all pages.

public string CRStatus
    {
        set { WorkflowSignoffStatus.Text = value; }
        get { return WorkflowSignoffStatus.Text; }
    }

Here is my Preliminary.aspx.cs

public partial class Preliminary : System.Web.UI.Page
public string WFLCRStatus
    {
        get
        {   object value = HttpContext.Current.Session["CRStatus"];
            return value == null ? "" : (string)value;
        }
        set
        { HttpContext.Current.Session["CRStatus"] = value;
        }
    } 
   protected void BtnToCCB_Click(object sender, EventArgs e)
   {
        WFLCRStatus = "In Review";
        Master.CRStatus = "In Review";
        Response.Redirect("CCB.aspx");
   }
}

Sets value to the label but on navigating to next page the label is empty.

I created a property here in the plan of using it in master.cs's Form_Load event to display the status. But I don't know how to use it there. Unable to create an instance there to access this property.


Solution

  • Calling redirect after setting a label's value makes no sense.

    Master.CRStatus = "In Review";
    Response.Redirect("CCB.aspx");
    

    When you redirect, the framework sends an HTTP Redirect to the client browser, and the current request/response cycle ends and a completely new one begins. Meaning the entire page lifecycle is loaded again, including the master page.

    To make this work, update your Session, perform the redirect, then in the Page_Load of the next page check the Session to see if that value is there, and update the label accordingly.

    WFLCRStatus.Status = "In Review"
    Response.Redirect("CCB.aspx");
    

    next page

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            if(!String.IsNullOrEmpty(Session["CRStatus"]))
            {
                Master.CRStatus = Session["CRStatus"].ToString();
            }
        }
    }