Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Answer to question 1:</strong></p> <p>IMHO, it really doesn't matter. I would say it's cleaner to use <code>Literal</code> when you don't need a label pointing to a field of a form, but, well, most people use <code>Label</code> in every case. Or output text directly with <code>&lt;% %&gt;</code>.</p> <p><strong>Answer to question 2:</strong></p> <p>There is no need to use sessions for this. Instead, use <code>this.Master</code> to access the Masterpage directly. You can then update the value of the label from your page.</p> <p>For example, do something like:</p> <pre><code>public class SiteMaster : MasterPage { public string StatusText { get; set; } } </code></pre> <p>and:</p> <pre><code>public class HomePage : Page { public void ChangeStatus(newStatus) { // Remember to cast this.Master to the class you use for the masterpage. ((SiteMaster)this.Master).StatusText = newStatus; } } </code></pre> <hr> <p>To answer your last questions:</p> <blockquote> <p>Do I need to copy this code onto every page in my site?</p> </blockquote> <p>Yes. <em>Unless</em> every page inherits from a parent class which inherits from <code>Page</code> class.</p> <blockquote> <p>What is SiteMaster?</p> </blockquote> <p><code>SiteMaster</code> is the class <em>I</em> gave to my master page. It could be <code>Template</code>, <code>Global</code>, or <em>whatever you want</em>.</p> <p>If it might be helpful, here's a full example of the thing:</p> <p><img src="https://i.stack.imgur.com/hNpE2.png" alt="Class diagram"></p> <p>File <strong>Site.Master</strong>:</p> <pre><code>&lt;%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Demos.StackOverflow.MasterPages.SiteMaster" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;Sample code for StackOverflow&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;a href="Default.aspx"&gt;Home page&lt;/a&gt; | &lt;a href="Products.aspx"&gt;Products&lt;/a&gt; &lt;div&gt; &lt;asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"&gt; &lt;/asp:ContentPlaceHolder&gt; &lt;/div&gt; &lt;div id="Status"&gt; &lt;%= this.StatusText %&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>File <strong>Site.Master.cs</strong>:</p> <pre><code>namespace Demos.StackOverflow.MasterPages { using System; using System.Web.UI; // Here's my SiteMaster class. public partial class SiteMaster : MasterPage { public string StatusText { get; set; } protected void Page_Load(object sender, EventArgs e) { } } } </code></pre> <p>File <strong>PageWithStatus</strong>:</p> <pre><code>namespace Demos.StackOverflow.MasterPages { using System.Web.UI; // This parent class inherits System.Web.UI.Page. This avoids code duplication. public class PageWithStatus : Page { protected void ChangeStatus(string newStatus) { ((SiteMaster)this.Master).StatusText = newStatus; } } } </code></pre> <p>File <strong>Default.aspx</strong>:</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Demos.StackOverflow.MasterPages.DefaultPage" %&gt; &lt;asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server"&gt; &lt;asp:Button runat="server" Text="Button" OnClick="DefaultButton_Click" /&gt; &lt;/asp:Content&gt; </code></pre> <p>File <strong>Default.aspx.cs</strong>:</p> <pre><code>namespace Demos.StackOverflow.MasterPages { using System; // Here, we inherits from PageWithStatus, not from Page. public partial class DefaultPage : PageWithStatus { protected void Page_Load(object sender, EventArgs e) { } protected void DefaultButton_Click(object sender, EventArgs e) { this.ChangeStatus("Done!"); } } } </code></pre> <p>File <strong>Products.aspx</strong>:</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Products.aspx.cs" Inherits="Demos.StackOverflow.MasterPages.ProductsPage" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"&gt; &lt;asp:Button runat="server" Text="Buy" onclick="BuyButton_Click" /&gt;&lt;br /&gt; &lt;asp:Button runat="server" Text="Sell" onclick="SellButton_Click" /&gt; &lt;/asp:Content&gt; </code></pre> <p>File <strong>Products.aspx.cs</strong>:</p> <pre><code>namespace Demos.StackOverflow.MasterPages { using System; // Here, we inherits from PageWithStatus, not from Page. public partial class ProductsPage : PageWithStatus { protected void Page_Load(object sender, EventArgs e) { } protected void BuyButton_Click(object sender, EventArgs e) { this.ChangeStatus("The product is purchased."); } protected void SellButton_Click(object sender, EventArgs e) { this.ChangeStatus("The product is sold."); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload