locked
Value of code behind variable not persisting RRS feed

  • Question

  • User1241411908 posted

    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:

    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

    Sunday, March 20, 2016 6:56 PM

Answers

  • User409696431 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 20, 2016 10:45 PM

All replies

  • User1241411908 posted

    Update:

    After some research I found information for viewstate. I changed my code to the following:

    namespace webTestVariable
    {
        public partial class wfrmMain : System.Web.UI.Page
        {
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (ViewState["bSwitch"] == null)
                    ViewState["bSwitch"] = false;
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                //Toggle value.
                ViewState["bSwitch"] = !Convert.ToBoolean(ViewState["bSwitch"]);
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                //if (bSwitch)
                Label1.Text = ViewState["bSwitch"].ToString();
            }
        }
    }

    It seems a bit clunky. Does this solution follow best practices, or is there a better way to accomplish this? Again, thanks! Saga

    Sunday, March 20, 2016 7:07 PM
  • User409696431 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 20, 2016 10:45 PM
  • User1241411908 posted

    Thank you KathyW! regards, Saga

    Sunday, March 20, 2016 11:02 PM
  • User-986267747 posted

    Hi sagav,

    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.

    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.

           static bool bSwitch = false;
    
            protected void Page_Load(object sender, EventArgs e)
            {
              
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
    
                bSwitch = !bSwitch;
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                Label1.Text = bSwitch.ToString();
            }

    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

    Monday, March 21, 2016 3:24 AM
  • User-158764254 posted

    You could also define the variable called bSwitch as a global variable

    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.

    Sunday, April 3, 2016 1:55 PM
  • User1241411908 posted

    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

    Sunday, April 3, 2016 7:07 PM