Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to Update or Change the Master Page Variables from a different Page?

Reply
Thread Tools

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

 
 
savvy
Guest
Posts: n/a
 
      06-05-2006
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

 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      06-05-2006
"savvy" <> wrote in message
news: oups.com...

> 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";


 
Reply With Quote
 
 
 
 
savvy
Guest
Posts: n/a
 
      06-05-2006
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

 
Reply With Quote
 
Ray Booysen
Guest
Posts: n/a
 
      06-05-2006
savvy wrote:
> 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();



>
> 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
>

 
Reply With Quote
 
savvy
Guest
Posts: n/a
 
      06-05-2006
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


Ray Booysen wrote:
> savvy wrote:
> > 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();
>
>
>
> >
> > 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
> >


 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      06-05-2006
"Ray Booysen" <> wrote in message
news:...

> savvy wrote:
>> 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();


Correct - apologies, my reply was from memory, and I should have actually
tested it first...


 
Reply With Quote
 
michaelcoleman72@gmail.com
Guest
Posts: n/a
 
      06-06-2006
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

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      06-06-2006
<> wrote in message
news: oups.com...

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

> 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
>



 
Reply With Quote
 
savvy
Guest
Posts: n/a
 
      06-07-2006
Thanks Micheal,
I followed the approach you gave
that perfectly fitted the bill
i had some problems with my previous approach
I got some more info from this link as well
http://www.odetocode.com/Articles/450.aspx
Thank you very much to all for your help and time


wrote:
> 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


 
Reply With Quote
 
adamsmith adamsmith is offline
Junior Member
Join Date: Jul 2007
Posts: 1
 
      07-03-2007
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.Cal lback(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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to Access Master Page Controls from page.aspx doesn't inherit from master SerpentKiss2010 ASP .Net 0 04-06-2011 02:46 PM
Setting the value of a Master page's control's property using a property of the Master page Nathan Sokalski ASP .Net 25 03-04-2010 03:42 AM
Can a master page be built from another master page Zeba ASP .Net 1 02-22-2007 10:55 AM
seeking servlet "Master" keep getting "Master/servlet/Master" not found. Tomcat 5.0.25 Doug McCann Java 1 08-05-2004 09:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57