Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Updating variables in Master Page

Updating variables in Master Page

Scheduled Pinned Locked Moved ASP.NET
helpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Munteanu Ciprian
    wrote on last edited by
    #1

    Hi everybody! I really need help... I have a Master Page with a menu and 2 Web Content Forms(called FirstWCF and SecondWCF). The menu contains two buttons used for Posting Back the two Web Content Forms. Now on my Master Page I have a label called Label1( Label1.Text="10") On FirstWCF I have a button and when I press it I want the Label1 to display 9, then 8 and so on until it displays 0. I've done this thing so far. The code is the next one:

    protected void Button1_Click(object sender, EventArgs e)
    {
    Label lbl=(Label)Master.FindControl("Label1");
    int tries=Convert.ToInt32(lbl.Text);
    if (tries>0)
    tries--;
    lbl.Text=tries.ToString();
    }

    This works just fine. When I'm on the FirstWCF and I press the button the label's text from the Master Page updates. But I got the next problem. When I load the SecondWCF I want the label's text to remain the same. Instead it has again the value "10". To be more clearly, I'm on the first page, I pressed 3 times the button (the label has the value 7), and then I press the menu item which loads the SecondWCF. The label's text becomes again 10 and I want to remain 7. Can anyone help me? I really need help....

    D 1 Reply Last reply
    0
    • M Munteanu Ciprian

      Hi everybody! I really need help... I have a Master Page with a menu and 2 Web Content Forms(called FirstWCF and SecondWCF). The menu contains two buttons used for Posting Back the two Web Content Forms. Now on my Master Page I have a label called Label1( Label1.Text="10") On FirstWCF I have a button and when I press it I want the Label1 to display 9, then 8 and so on until it displays 0. I've done this thing so far. The code is the next one:

      protected void Button1_Click(object sender, EventArgs e)
      {
      Label lbl=(Label)Master.FindControl("Label1");
      int tries=Convert.ToInt32(lbl.Text);
      if (tries>0)
      tries--;
      lbl.Text=tries.ToString();
      }

      This works just fine. When I'm on the FirstWCF and I press the button the label's text from the Master Page updates. But I got the next problem. When I load the SecondWCF I want the label's text to remain the same. Instead it has again the value "10". To be more clearly, I'm on the first page, I pressed 3 times the button (the label has the value 7), and then I press the menu item which loads the SecondWCF. The label's text becomes again 10 and I want to remain 7. Can anyone help me? I really need help....

      D Offline
      D Offline
      Dinesh Mani
      wrote on last edited by
      #2

      This has got nothing to do with the Master page! When you use master pages for headers and footers it just gives you a template for you to implement a common design for your header, footer, navigation, etc. The master page is not shared among pages, but each page gets a new object of the master page. So, by hard coding the value for the label in the master's mark-up you cannot achieve what you desire. But, this is not so hard to achieve. Just have a session variable, if the clicks are user specific and on session start initialize it to 10 or what ever value you need. Then assign this value to the label on the master page. In the content pages, just update the value on the session variable and display the updated value on the master label So your code can be re-written like this -

      protected void Button1_Click(object sender, EventArgs e)
      {
      int tries=Convert.ToInt32(Session["Tries"].ToString());
      if (tries>0)
      {
      Session["Tries"]=--tries;
      (Label)Master.FindControl("Label1").Text=tries.ToString();
      }
      }

      M 1 Reply Last reply
      0
      • D Dinesh Mani

        This has got nothing to do with the Master page! When you use master pages for headers and footers it just gives you a template for you to implement a common design for your header, footer, navigation, etc. The master page is not shared among pages, but each page gets a new object of the master page. So, by hard coding the value for the label in the master's mark-up you cannot achieve what you desire. But, this is not so hard to achieve. Just have a session variable, if the clicks are user specific and on session start initialize it to 10 or what ever value you need. Then assign this value to the label on the master page. In the content pages, just update the value on the session variable and display the updated value on the master label So your code can be re-written like this -

        protected void Button1_Click(object sender, EventArgs e)
        {
        int tries=Convert.ToInt32(Session["Tries"].ToString());
        if (tries>0)
        {
        Session["Tries"]=--tries;
        (Label)Master.FindControl("Label1").Text=tries.ToString();
        }
        }

        M Offline
        M Offline
        Munteanu Ciprian
        wrote on last edited by
        #3

        Thanks a lot! But how to do that:"on session start initialize it to 10". In what page's code should I do this?

        D 1 Reply Last reply
        0
        • M Munteanu Ciprian

          Thanks a lot! But how to do that:"on session start initialize it to 10". In what page's code should I do this?

          D Offline
          D Offline
          Dinesh Mani
          wrote on last edited by
          #4

          Use the Session_Start event in Global.asax

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups