Viewstate should never get expired RRS feed

  • Question

  • User926484134 posted


    i have a situation in what the user will keep a page/form open for couple of hours/days. After that time when the user comes to the page, even if i check cookies, i get other login variables, but the state of the controls is lost. Now when user clicks refresh, instead of postback, a fresh request is made.

    I have read a lot and found that the viewstate is itself stored in the session. so when the session expires the viewstate is gone. 

    How do i retain the viewstate, so that the selections remain the same the page refreshes.

    Now i cannot store the current values of the selected fields in cookies as i have to apply this logic to a hell lot of pages.

    Lets first talk about only Single page.

    The code below is useless, but is something that i was using.

    if (ViewState["AdminFlag"] != null)
                    Session["AdminFlag"] = ViewState["AdminFlag"].ToString();
                if (Session["AdminFlag"] != null)
                Admin_Flag = Session["AdminFlag"].ToString();
                Master.ChkLogin();//checks cookies and populates to session
                if (!IsPostBack)

    Friday, January 2, 2015 12:44 AM

All replies

  • User1083584480 posted

    In that case, you need to save your session in database instead of in memory. your session will never get expired. you need to serialize the object saving in session or viewstate.

    for detail information please look at the following link. http://www.itorian.com/2012/09/aspnet-session-states-in-sql-server.html


    Friday, January 2, 2015 1:23 AM
  • User926484134 posted

    Hi sumit, Thanks for replying.

    Is there any other way too?

    because storing into the database would add some more overheads.

    (How can we do a viewstate Never expire?) How many diffrent ways are there.?

    Friday, January 2, 2015 4:04 AM
  • User-158764254 posted

    I have read a lot and found that the viewstate is itself stored in the session.

    By default, Viewstate is stored in a hidden field that is part of the page and sent to the client.  There are ways to force it to be stored in Session but that's not the default.

    Friday, January 2, 2015 12:15 PM
  • User926484134 posted


    so i am wrong. thanks to point out. could you please enlighten a little more on that.

    As it would be stored in hidden field, and will be part of the page, what will happen of it when the page is inactive for a certain amount of time.

    What should happen of the same if the user comes back the next day with the same page open and just clicks on certain button on the same page.

    What is happening in my case is that the page is firing a fresh refresh request instead of a postback!

    Monday, January 12, 2015 5:19 AM
  • User724169276 posted

    the page is firing a fresh refresh request instead of a postback!

    How did you come to know that its a fresh request not the postback ?

    Monday, January 12, 2015 5:57 AM
  • User926484134 posted

    i check


    i check the statements with the help of breakpoints

    Monday, January 12, 2015 9:41 AM
  • User-760709272 posted

    There is something happening that you're not telling us, such as the user being directed to a login page when they submit the form after a period of time then coming back to the form, or something like that.  I can't see how what you're describing is possible.

    Monday, January 12, 2015 9:50 AM
  • User926484134 posted

    hi AidyF,

    nice to see you again,

    no, nothing is happening, as i have shared little code earlier,

    I will elaborate it once again,

    if (ViewState["AdminFlag"] != null)
                    Session["AdminFlag"] = ViewState["AdminFlag"].ToString();
                if (Session["AdminFlag"] != null)
                Admin_Flag = Session["AdminFlag"].ToString();

    1] i want the AdminFlag to be retained, the viewstate to be retained.

    2] if the user keeps the page open on his/her browser, again opens the tab and clicks the refresh data button, only the data in the grid should get refreshed as  the user has already selected other details from the dropdowns.(I have few selection fields like dropdown and date and a refresh button that refreshes the data)

    3] Once done with this logic, i have to apply this logic all over my web application!.

    I do have a function 

    if (Session["UsrID"] == null && HttpContext.Current.Request.Cookies["comp"] != null)
                    Session["UsrID"] = HttpContext.Current.Request.Cookies["comp"]["UsrID"].ToString();
                    Session["AdmnFlg"] = HttpContext.Current.Request.Cookies["comp"]["AdmnFlag"].ToString();
                    Session["usrNam"] = HttpContext.Current.Request.Cookies["comp"]["UsrN"].ToString();
                    BLCust BLC = new BLCust();
                    string strMg = BLC.SaveLognDta(Session["UsrID"].ToString());
                if (Session["UsrID"] == null && ViewState["UsrID"] != null)
                    Session["UsrID"] = ViewState["UsrID"];
                    Session["AdmnFlag"] = ViewState["AdmnFlg"];
                    Session["usrNam"] = ViewState["usrNm"];
                if (Session["UsrID"] == null)

    This code i fire later on, so i feel  it has no effect, the first thing is i should get adminflag in the second line for the former statements.

    Please help.

    Monday, January 12, 2015 10:22 AM