locked
Retain values during page postback - List gets initialized every postback RRS feed

  • Question

  • Hi all

    I am stuck again, but now with retaining the values during page postback. You see, everytime I click on a button or any control that has event handler tied to it, the page postbacks and the list gets initialized everytime resulting in the data that was earlier added to the list gets emptied.

    I have done some reading and realise that this is because of the page lifecycle and there are few ways to overcome it, we can either declare the list as static, use view state or session. 

    But I read that view state would cause an issue when the data becomes large. What are the pros and cons of each technique?

    What's the best way forward, I declared the list at the class and not in any of the method. This post was earlier linked to 

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/7e91a995-5946-4302-b3cc-f25fe951e4e5/how-to-call-a-list-created-in-an-event-handler-best-practices-in-creating-list-and-storing-them-in?forum=csharpgeneral#de9ff008-77e5-4dff-aea8-d462775d4aff 


    • Edited by cally_kalpana Friday, January 25, 2019 1:04 AM
    • Moved by CoolDadTx Friday, January 25, 2019 2:50 PM ASP.NET related
    Friday, January 25, 2019 1:03 AM

Answers

  • Here is a simple way to write the saving and reading of the list from the Session:

    private List<DateTime> A_Week_1
    {
        get { return Session["aweek1"] as List<DateTime>; }
        set { Session["aweek1"] = value; }
    }

    Drawbacks of using the Session:

    • It will occupy memory in the server. If the list contains one megabyte of data, and this is done by every user of your website, you will use up as many megabytes as users.
    • It needs the Session cookie to be accepted by the user's browser. If the user disables all cookies, the session will not work.
    • If you ever deploy the application in a web farm, you will need to move the session to a separate shared session server or database server so that all web servers can see it. This will slow down access to the session, and also requires all objects in the Session to be Serializable (which is not a problem in your case because the List<DateTime> is Serializable).

    • Marked as answer by cally_kalpana Monday, January 28, 2019 12:31 AM
    Friday, January 25, 2019 7:27 AM

All replies

  • I have done some reading and realise that this is because of the page lifecycle and there are few ways to overcome it, we can either declare the list as static, use view state or session. 

    But I read that view state would cause an issue when the data becomes large. What are the pros and cons of each technique?

    Session state or caching is the optimal approach.  

    Viewstate, means that the data is making the roundtrip between the browser and the Web server, and if it's a lot of data being dragged around, it can really affect Web application performance. 

    Your other ways to keep state.

    https://www.codeproject.com/Articles/331962/A-Beginner-s-Tutorial-on-ASP-NET-State-Management

    the list as static

    I don't know what you are talking about. Maybe, you are talking about an HTML Dropdown list that has static data loaded into it during control materialization.

    ASP.NET issues can be discussed at the ASP.NET forums.

    https://forums.asp.net/

    Friday, January 25, 2019 2:00 AM
  • Thanks DA924x, what I meant as static was , adding the static keyword in the List declaration. However, I read that, it could cause problems when 2 users are viewing the same page as the same data would be displayed at both end. 

    Something like this 

    public static List<DateTime> A_Week_1

    Friday, January 25, 2019 2:31 AM
  • Thanks DA924x, what I meant as static was , adding the static keyword in the List declaration. However, I read that, it could cause problems when 2 users are viewing the same page as the same data would be displayed at both end. 

    Something like this 

    public static List<DateTime> A_Week_1

    https://www.dotnetperls.com/static-list

    When multiple threads are used, static Lists can be problematic. 

    Web solutions are multi thread solutions. So no, it should not  be used.


    • Edited by DA924x Friday, January 25, 2019 4:23 AM
    Friday, January 25, 2019 4:22 AM
  • Hi cally_kalpana,

    Thank you for posting here.

    For your question, what does the page postback mean? Do you mean you want to save the value every time?

    Could you provide the results about what you want?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 25, 2019 6:08 AM
  • Hi cally_kalpana,

    Thank you for posting here.

    For your question, what does the page postback mean? Do you mean you want to save the value every time?

    Could you provide the results about what you want?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    https://www.codeproject.com/Articles/811684/Understanding-The-Complete-Story-of-Postback-in-AS


    Friday, January 25, 2019 6:57 AM
  • Here is a simple way to write the saving and reading of the list from the Session:

    private List<DateTime> A_Week_1
    {
        get { return Session["aweek1"] as List<DateTime>; }
        set { Session["aweek1"] = value; }
    }

    Drawbacks of using the Session:

    • It will occupy memory in the server. If the list contains one megabyte of data, and this is done by every user of your website, you will use up as many megabytes as users.
    • It needs the Session cookie to be accepted by the user's browser. If the user disables all cookies, the session will not work.
    • If you ever deploy the application in a web farm, you will need to move the session to a separate shared session server or database server so that all web servers can see it. This will slow down access to the session, and also requires all objects in the Session to be Serializable (which is not a problem in your case because the List<DateTime> is Serializable).

    • Marked as answer by cally_kalpana Monday, January 28, 2019 12:31 AM
    Friday, January 25, 2019 7:27 AM
  • Please post questions related to ASP.NET in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, January 25, 2019 2:50 PM