Updating variables in Master Page
-
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....
-
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....
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();
}
} -
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();
}
}Thanks a lot! But how to do that:"on session start initialize it to 10". In what page's code should I do this?
-
Thanks a lot! But how to do that:"on session start initialize it to 10". In what page's code should I do this?
Use the
Session_Start
event in Global.asax