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...
 
Joined
Jul 3, 2007
Messages
1
Reaction score
0
Changing a variable passed to a usercontrol from a different page?

Hi All, hope you can help me. I'm trying to change a variable which is passed to a usercontrol from a masterpage. This is when a asp:content holder page is loaded from within a masterpage.

I have the following master page called main.master:

<%@ Master Language="C#" %>

<%@ Register TagPrefix="DW" TagName="MyMenu" Src="~/UserControls/SubMenu.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

public partial class main : System.Web.UI.MasterPage
{
public UserControls_Menu MyMenu = new UserControls_Menu();
}


</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Test</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id=main>
<form id="form1" runat="server">
<div id=subpagemenudiv>
<DW:MyMenu ID="MyMenu1" runat="server" MenuToLoad="product"/>
</div>
</form>
</body>
</html>

and.... the asp content holder page which uses the main.master page:

<%@ Page Language="C#" MasterPageFile="~/main.master" Title="Untitled Page" %>

<%@ Register TagPrefix="DW" TagName="MyMenu" Src="~/UserControls/SubMenu.ascx" %>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
((ASP.main_master.main)Master).MyMenu.MenuToLoad = UserControls_Menu.SiteMapMenus.main;
}

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

</asp:Content>


The user control loads a different sitemap depending on what MenuToLoad is set too. So in the main.master it is "product" and in the content holder page I want it to be changed to "main".

The website loads fine but errors when I try to load the page which sets the menu control to "main". It errors the following:

System.InvalidCastException was unhandled by user code
Message="Unable to cast object of type 'ASP.main_master' to type 'main'."
Source="App_Web_1ehcrpfr"
StackTrace:
at ASP.company_default_aspx.Page_Load(Object sender, EventArgs e) in c:\Documents and Settings\smithad\My Documents\Visual Studio 2005\WebSites\Interlink\Company\Default.aspx:line 9
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Has anybody any ideas how I can change the master page variable which I pass to this user control, from the asp content page?

Thanks
Adam
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top