How to Update or Change the Master Page Variables from a different Page?

S

savvy

I'm developing a shopping cart. I've assigned some Session values to
Labels on the Master Page. The Basket panel which is small window for
the basket items will be visible on every page if there are any items
in the basket. It will display Total Quantity and Total Price in that
window. The Master Page code shown below

protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToString(Session["Total"]) != string.Empty)
{
pllogin.Visible = false;
plbasket.Visible = true;
lblquantity.Text = Convert.ToString(Session["TotQty"]);
lblprice.Text = Convert.ToString(Session["Total"]);
}
}

Hope i made myself clear till now.
In the basket Page , i'm calculating the total Price and Quantity and
storing them in Sessions as shown below
Session["Total"] = GetProductTotal();
Session["TotQty"] = intQty;
So, basically on the basket page when i press the Update button I want
the Master page values to get updated and be displayed in the small
window which is not happening straight away, So I need to refresh the
page to get it updated, which is not what i want.
Should I be using any classes (.cs) outside the page ?
Is there anyway to get over this problem ?
Am i going in the right directions ?
Thanks for your help and time in Advance
 
M

Mark Rae

Should I be using any classes (.cs) outside the page ?

No need.
Is there anyway to get over this problem ?
Yes.

Am i going in the right directions ?

Sort of...

Let's say you have a master file called secure.master with an associated
partial class called master_secure.

public partial class master_secure : System.Web.UI.MasterPage
{
public Label MyLabel;
}

To refer to the label from a content placeholder, you would do the
following:

((master_secure)Master).MyLabel.Text = "Hello world";
 
S

savvy

Thank you very much Mark for your time and help
I tried to do this
public partial class Home1 : System.Web.UI.MasterPage
{
public Label lblqty;
}

and on the other page if i'm trying to assign the lblqty as
protected void Page_Load(object sender, EventArgs e)
{
((Home1)Master).lblqty.Text = "Hello World";
}
its giving an error message saying
Object reference not set to an instance of an object.
i'm not able to understand where the problem lies
Can you help, Thanks
 
R

Ray Booysen

savvy said:
Thank you very much Mark for your time and help
I tried to do this
public partial class Home1 : System.Web.UI.MasterPage
{
public Label lblqty;
}
Should be:

public Label lblqty = new Label();
 
S

savvy

Thank you very very much mates
I'm really greatful to u lot
That worked perfectly and was a perfect solution to my problem
thanks once again
 
M

michaelcoleman72

Better yet so you don't have to cast your master page each time for
access in your pages' code behinds, add the following to the .aspx
file. (You can also set in the web config for all pages in a site, but
I do not like to do this in case you wish for a page not to use the
master.)

<%@ MasterType TypeName="Base_master" %>

where Base_master is the name of the master page code behind you are
trying to reference.

You can then access public properties and methods from the master in
the page using it.

In the master:

string m_JobTitle;
public string Job
{
get { return m_JobTitle; }
set { m_JobTitle= value;}
}

In your .aspx.cs

Master.JobTitle= "Professional Hack";

Regards
Coleman
 
M

Mark Rae

And even better than that, make "Base_master" a separate class which
inherits MasterPage and derive all your master pages from that...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top