I created a simple empty ASP.NET Web forms project. I added one Web form (no Master page)
I added two Buttons and one label to the form. Button1.Text = "Toggle" and Button2.Text = "Test"
My code behind, in C#, is the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace webTestVariable
{
public partial class wfrmMain : System.Web.UI.Page
{
bool bSwitch = false;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//Toggle value.
bSwitch = !bSwitch;
}
protected void Button2_Click(object sender, EventArgs e)
{
//Display value of Switch
Label1.Text = bSwitch.ToString();
}
}
}
Button1 just toggles the Boolean variable and Button2 displays the variable's value in a label. I added two breakpoints, the first in the Button1 click event where the variable is toggled and the second in the Button2 click event where the value of bSwitch
is assigned to the label.
When I run the project and click the Toggle button I can see that the variable is successfully toggled to True, but when I click the second button to display the value I can see that the variable's value is back to false. To me it is obvious that I am still
not understanding how the process flows. I just started learning ASP.NET and have been experimenting with CSS, JavaScript and C# and have been exploring different aspects. Can someone please point me in the right direction? What do I need to do for the variable's
value to persist from when Button1 is clicked to when Button2 is clicked? Thanks! Saga
ViewState is perfectly fine for persisting values after postbacks. Especially in this case, where the data you are saving is very small and won't have any significant impact on the page size.
What do I need to do for the variable's value to persist from when Button1 is clicked to when Button2 is clicked? Thanks! Saga
According to your code, the problem here is lifetime of that variable, That will be destroyed just as soon as the page is done processing. Next time you attempt to do something with it, it's back to original value. the variable called bSwitch is not a global
variable.
sagav
It seems a bit clunky. Does this solution follow best practices, or is there a better way to accomplish this? Again, thanks! Saga
You could also define the variable called bSwitch as a global variable. I create a sample and it works fine.
Besides, you could also save the variable to cookie or session, it still works fine. You could refer to the following link to get more information about this similar problem.
You could also define the variable called bSwitch as a global variable
Klein Zhang
static bool bSwitch = false;
It should be pointed out though that this is most often an incredibly bad idea given that all users of the website when then be sharing that one single variable.
Thanks for your insight. What you say about it being a bad idea is important. I am just learning ASP.NET and have been struggling with the concept of accessing variables across the website.
For example. I have a Master page. In that page's code behind I define a static variable and then use that as the back up for a public property. I want to be able to get or set that variable's value from any of the APSX pages that the website visitor goes
to. However, you say that this not only affects that particular visitor, but all the Website's visitors. That definitely is not good.
I had thought of creating a code behind C# class to hold the Website's data; however, if this also affects all visitors then I am not doing this correctly.
I need to take a step back to understand how ASP.NET works and how I can create a Website resource that is updateable and accessible from all the Website's pages without affecting all visitors. I understand the concept of code behind, but obviously do not
understand it fully enough.
What methods can you suggest that I use to have a centralized data store for the Website that holds data that can be updated and accessed by any page?
Member
3 Points
20 Posts
Value of code behind variable not persisting
Mar 20, 2016 06:56 PM|sagav|LINK
Hi all,
I have another question.
I created a simple empty ASP.NET Web forms project. I added one Web form (no Master page)
I added two Buttons and one label to the form. Button1.Text = "Toggle" and Button2.Text = "Test"
My code behind, in C#, is the following:
Button1 just toggles the Boolean variable and Button2 displays the variable's value in a label. I added two breakpoints, the first in the Button1 click event where the variable is toggled and the second in the Button2 click event where the value of bSwitch is assigned to the label.
When I run the project and click the Toggle button I can see that the variable is successfully toggled to True, but when I click the second button to display the value I can see that the variable's value is back to false. To me it is obvious that I am still not understanding how the process flows. I just started learning ASP.NET and have been experimenting with CSS, JavaScript and C# and have been exploring different aspects. Can someone please point me in the right direction? What do I need to do for the variable's value to persist from when Button1 is clicked to when Button2 is clicked? Thanks! Saga
Member
3 Points
20 Posts
Re: Value of code behind variable not persisting
Mar 20, 2016 07:07 PM|sagav|LINK
Update:
After some research I found information for viewstate. I changed my code to the following:
It seems a bit clunky. Does this solution follow best practices, or is there a better way to accomplish this? Again, thanks! Saga
Contributor
5961 Points
2466 Posts
Re: Value of code behind variable not persisting
Mar 20, 2016 10:45 PM|KathyW|LINK
ViewState is perfectly fine for persisting values after postbacks. Especially in this case, where the data you are saving is very small and won't have any significant impact on the page size.
Member
3 Points
20 Posts
Re: Value of code behind variable not persisting
Mar 20, 2016 11:02 PM|sagav|LINK
Thank you KathyW! regards, Saga
Star
8460 Points
1445 Posts
Re: Value of code behind variable not persisting
Mar 21, 2016 03:24 AM|Klein Zhang|LINK
Hi sagav,
According to your code, the problem here is lifetime of that variable, That will be destroyed just as soon as the page is done processing. Next time you attempt to do something with it, it's back to original value. the variable called bSwitch is not a global variable.
You could also define the variable called bSwitch as a global variable. I create a sample and it works fine.
Besides, you could also save the variable to cookie or session, it still works fine. You could refer to the following link to get more information about this similar problem.
http://forums.asp.net/p/2084053/6016687.aspx
Best Regards,
Klein zhang
All-Star
160043 Points
13198 Posts
ASPInsiders
Moderator
Re: Value of code behind variable not persisting
Apr 03, 2016 01:55 PM|mbanavige|LINK
It should be pointed out though that this is most often an incredibly bad idea given that all users of the website when then be sharing that one single variable.
Member
3 Points
20 Posts
Re: Value of code behind variable not persisting
Apr 03, 2016 07:07 PM|sagav|LINK
Thanks for your insight. What you say about it being a bad idea is important. I am just learning ASP.NET and have been struggling with the concept of accessing variables across the website.
For example. I have a Master page. In that page's code behind I define a static variable and then use that as the back up for a public property. I want to be able to get or set that variable's value from any of the APSX pages that the website visitor goes to. However, you say that this not only affects that particular visitor, but all the Website's visitors. That definitely is not good.
I had thought of creating a code behind C# class to hold the Website's data; however, if this also affects all visitors then I am not doing this correctly.
I need to take a step back to understand how ASP.NET works and how I can create a Website resource that is updateable and accessible from all the Website's pages without affecting all visitors. I understand the concept of code behind, but obviously do not understand it fully enough.
What methods can you suggest that I use to have a centralized data store for the Website that holds data that can be updated and accessed by any page?
Thanks! Saga