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

Discussion in 'ASP .Net' started by savvy, Jun 5, 2006.

  1. savvy

    savvy Guest

    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
     
    savvy, Jun 5, 2006
    #1
    1. Advertising

  2. savvy

    Mark Rae Guest

    "savvy" <> wrote in message
    news:...

    > 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";
     
    Mark Rae, Jun 5, 2006
    #2
    1. Advertising

  3. savvy

    savvy Guest

    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
     
    savvy, Jun 5, 2006
    #3
  4. savvy

    Ray Booysen Guest

    Re: How to Update or Change the Master Page Variables from a differentPage?

    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
    >
     
    Ray Booysen, Jun 5, 2006
    #4
  5. savvy

    savvy Guest

    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
    > >
     
    savvy, Jun 5, 2006
    #5
  6. savvy

    Mark Rae Guest

    "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...
     
    Mark Rae, Jun 5, 2006
    #6
  7. savvy

    Guest

    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
     
    , Jun 6, 2006
    #7
  8. savvy

    Mark Rae Guest

    <> wrote in message
    news:...

    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
    >
     
    Mark Rae, Jun 7, 2006
    #8
  9. savvy

    savvy Guest

    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
     
    savvy, Jun 7, 2006
    #9
  10. savvy

    adamsmith

    Joined:
    Jul 3, 2007
    Messages:
    1
    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
     
    adamsmith, Jul 3, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug McCann
    Replies:
    1
    Views:
    1,030
    William Brogden
    Aug 5, 2004
  2. Zeba
    Replies:
    1
    Views:
    495
    Mark Rae
    Feb 22, 2007
  3. katmagic
    Replies:
    0
    Views:
    1,257
    katmagic
    Dec 26, 2008
  4. Nathan Sokalski
    Replies:
    25
    Views:
    1,437
    Nathan Sokalski
    Mar 4, 2010
  5. SerpentKiss2010
    Replies:
    0
    Views:
    2,494
    SerpentKiss2010
    Apr 6, 2011
Loading...

Share This Page