locked
maintain variable value across page loads RRS feed

  • Question

  • User-399355216 posted
    i have the following code in an asp.net web app:
    public partial class _Default : System.Web.UI.Page
        {
            int x;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    x = 5;
                }
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                x = 6;
            }
        }
    the first time the page is loaded x is set to 5. when i press Button1, Page_Load gets called again, but the value for x is 0, as if it hadn't been initialised. how can i maintain x's value across page loads?
    thank u
    Tuesday, July 12, 2011 5:57 AM

Answers

  • User-1142685999 posted

    data will keep flowing into my Stream

    One thing to consider if you are using the Application object is that any writes to it should be done either in its Application_OnStart event (in global.asax) or within an Application.Lock section. While using Application.Lock is necessary to ensure that writes are performed properly, it also serializes requests for the Application object, which can be a serious performance bottleneck for the application.

    public partial class _Default : System.Web.UI.Page
            {
                int someValue;
                protected void Page_Load(object sender, EventArgs e)
                {
                    if (!IsPostBack)
                    {
                        Application.Lock();
                        Application["X"] = someValue;
                        Application.UnLock();
                    }
                }
    
                protected void Button1_Click(object sender, EventArgs e)
                {
                    int someNewValue = 123; // GET from HttpWebRequest/HttpWebResponse
                    Application.Lock();
                    Application["X"] = someNewValue;
                    Application.UnLock();
                }
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 12, 2011 7:29 AM
  • User-1561814533 posted

    The following article will help you understand the caching; http://www.eggheadcafe.com/articles/20030405.asp

    A combination of page level caching (so your page does not get recreated every time and so improves performance) and object caching may be appropriate in this case.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 12, 2011 8:54 AM

All replies

  • User905936915 posted

    make it a STATIC variable

    Tuesday, July 12, 2011 6:00 AM
  • User905936915 posted
    public partial class _Default : System.Web.UI.Page
        {
           Static int x;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    x = 5;
                }
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                x = 6;
            }
        }
    Tuesday, July 12, 2011 6:00 AM
  • User-1561814533 posted

    make it a STATIC variable

    Don't do this unless youi want the variable scope to be application rather than session wide.

    ASP.NET is a stateless technology, everytime you access the server it starts afresh. If you want to 'remember' a value between trips to the server you will need to store it in either view state or session state.

    Note, that is you store it in a static variable or in application state then it could be updated by any user session. That may or may not be what you desire in this case.

    Tuesday, July 12, 2011 6:07 AM
  • User-366017857 posted

    Hi use ViewState to store value

    ViewState is the mechanism that allows state values to be preserved across page postbacks.

    if (!IsPostBack)

                {
                   ViewState["x"] = 5;
                }
    protected void Button1_Click(object sender, EventArgs e)
            {
                ViewState["x"] = 6;
            }
    Tuesday, July 12, 2011 6:26 AM
  • User-399355216 posted

    thank u for your answers. and let me make it more specific. 

    the variables i need to maintain are an HttpWebRequest, HttpWebResponse, a Stream that is continuously fed with input data from a server and an associated StreamReader. these need to be instantiated the first time the page is loaded.

    the page contains a gridview that binds to its datasource every other n seconds, triggering a PostBack every time (this is when my variables lose their initial values).

    for this setting, from what i read here http://msdn.microsoft.com/en-us/magazine/cc300437.aspx i think i need to store my variables in an Application object, because the variables are not session specific (if two users access the page, they should see the same thing), can reach a great storage size and will be available the whole time the application runs. and apparently the user browser is not required to enable cookies.

    please correct me if i am wrong.

    thanks,

    s

    Tuesday, July 12, 2011 6:29 AM
  • User-399355216 posted

    yes, but deosn't ViewState store data for a user session?

    Tuesday, July 12, 2011 6:33 AM
  • User-1142685999 posted

    the variables are not session specific (if two users access the page, they should see the same thing)

    i think you need to use Cache for this

    Tuesday, July 12, 2011 6:35 AM
  • User-1142685999 posted

    ViewState is specific for a Page ONLY. 

    NOT for User Session.

    If user moves to next page, ViewState will be expired.

    Where as, it is not recommended to use Session variables for huge data objects

    However, in your case, you does not want to store User Specific data, then even Session variables will be useless

    Application level Objects are initialized only ONCE, they retain their value until application is restarted.

    Hence USELESS in this case

    So i think CACHE would the thing you will need to use.

    Tuesday, July 12, 2011 6:37 AM
  • User-399355216 posted

    "Application level Objects are initialized only ONCE, they retain their value until application is restarted."

    This is the behaviour i need. I need to initialise my variables once, when the application starts and maintain those values as long as the web page lives. As long as the connection with my server is opened, data will keep flowing into my Stream.

    So why Cache and not Application?

    (Thanks for clearifying ViewState, however i still wouldn'tuse it since it's page specific. )

    Tuesday, July 12, 2011 6:59 AM
  • User-1714886539 posted

    {

            int x;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack=false)
                {
                    x = 5;
                }
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                x = 6;
            }
        }
    Tuesday, July 12, 2011 7:07 AM
  • User-1714886539 posted

    hi,

    Use hidden field to store the value.

    Use label to store the value.

    Use get set property to store the value.

    Tuesday, July 12, 2011 7:09 AM
  • User-1142685999 posted

    data will keep flowing into my Stream

    One thing to consider if you are using the Application object is that any writes to it should be done either in its Application_OnStart event (in global.asax) or within an Application.Lock section. While using Application.Lock is necessary to ensure that writes are performed properly, it also serializes requests for the Application object, which can be a serious performance bottleneck for the application.

    public partial class _Default : System.Web.UI.Page
            {
                int someValue;
                protected void Page_Load(object sender, EventArgs e)
                {
                    if (!IsPostBack)
                    {
                        Application.Lock();
                        Application["X"] = someValue;
                        Application.UnLock();
                    }
                }
    
                protected void Button1_Click(object sender, EventArgs e)
                {
                    int someNewValue = 123; // GET from HttpWebRequest/HttpWebResponse
                    Application.Lock();
                    Application["X"] = someNewValue;
                    Application.UnLock();
                }
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 12, 2011 7:29 AM
  • User-1561814533 posted

    The following article will help you understand the caching; http://www.eggheadcafe.com/articles/20030405.asp

    A combination of page level caching (so your page does not get recreated every time and so improves performance) and object caching may be appropriate in this case.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 12, 2011 8:54 AM
  • User1225026217 posted

    Or better construction is:

    int x

    {

        get

        {

            if(ViewState["x"] is int)

            {

                reruen (int)ViewState["x"];

            }

           return 0;

        }

        set

       {

         ViewState["x"] = value;

       }

    }

    This way you can use x as an usual variable. But if you reload the page not via postback (via direct link clicking, or via Response.Redirect) then its value will be lost.

    Friday, July 15, 2011 8:08 AM