locked
Keep data after postback RRS feed

  • Question

  • User1449393024 posted

    Hi there.

    I wanted to keep 1 data after postbacks.

    I tried Viewstate but it will be deleted after postback.

    what's the best approach for this?

    Thursday, April 18, 2013 3:19 AM

Answers

  • User541108374 posted

    Viewstate is a per page per user way for a web form to keep data in sync. You can inject things into viewstate and get them back on a postback, for example in a Page_Load event. If you go earlier in the page life cycle it might not have been restored yet.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2013 3:44 AM
  • User541108374 posted

    Hi,

    refresh the page with another querystring and BEFORE the 4th line of code I see that viewstate is empty!

    Since this piece of code's in a check of IsPostback I'm wondering how you refresh the page. Normally on a postback you shouldn't get into that particular piece of code and your viewstate should be in order.

    I can only guess but likely you render out your page a first time, then change the querystring in the address bar and hit enter. Know that this isn't a postback but a new page request hence you've lost your viewstate.

     

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2013 9:22 AM

All replies

  • User1610357552 posted

    try session insteed of Viewstate

    Thursday, April 18, 2013 3:32 AM
  • User541108374 posted

    Hi,

    you can go for cookies, session or querystring if it's data on a per user basis.

    Grz, Kris.

    Thursday, April 18, 2013 3:32 AM
  • User1449393024 posted

    So what's the usage of ViewState?

    Thursday, April 18, 2013 3:36 AM
  • User541108374 posted

    Viewstate is a per page per user way for a web form to keep data in sync. You can inject things into viewstate and get them back on a postback, for example in a Page_Load event. If you go earlier in the page life cycle it might not have been restored yet.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2013 3:44 AM
  • User1449393024 posted

    So why you adviced me to use session?

    I wanted to keep a value between postback in a page. but I don't know why after 1 postback the viewstate became empty.

    Thursday, April 18, 2013 4:43 AM
  • User541108374 posted

    Hi,

    ehsansh

    So why you adviced me to use session?

    It's an alternative just like the others I talked about.

    ehsansh

    I don't know why after 1 postback the viewstate became empty

    In which event did you set the viewstate and in which event did you want to retrieve it? In the Page Life Cycle of a webform it's essentials to do certain things before or after a specific event in order to have things working.

    Something else: can you check if viewstate's turned on for your page or web application? Know that it can be turned off in several places like web.config, page level, code behind, ...

    Grz, Kris.

    Thursday, April 18, 2013 5:03 AM
  • User-1768369891 posted

    Hi,  try this

    I can't find why the viewstate did not persist after postback, but I've found another way to hold the value using the controlstate. Here's my implementation:

    Public Property SortTime() As DateTime Get Return _SortTime End Get Set(ByVal value As DateTime) _SortTime = value End Set End Property

    Add the following overridden methods:

     Protected Overrides Sub OnInit(ByVal e As System.EventArgs) Page.RegisterRequiresControlState(Me) MyBase.OnInit(e) End Sub Protected Overrides Function SaveControlState() As Object Return _SortTime End Function Protected Overrides Sub LoadControlState(ByVal savedState As Object) Dim state As DateTime = CType(savedState, DateTime) Me._SortTime = state End Sub

    Give a value to the property in OnLoad

     Protected Overrides Sub OnLoad(ByVal e As System.EventArgs) If Not Page.IsPostBack Then SortTime = DateTime.Now End If End Sub

    Now i can call the property after the page has loaded and it will always have the value set on the first page load. It will not change on postback and it won't be gone on postback.

    I'm sure this is possible with the viewstate too, but for some reason, this was not working for me.

    or may be

    OK, I fixed this problem. I think the ViewState is just something like a Dictionary that keep a list of key value pair. In my case, my value is objects, which is a address or reference. So when I get a object from ViewState, and uncarefully assign new children to that object. all new children will be put to ViewState automatically. Unfortunately, in my code there is really uncareful cycle that get children from ViewState, create copies in memory and add them back! The code cannot distinguish duplicate children because they are at different address.... So finally the page get 2 times bigger at each post back and bang!!!!!!!

    http://msdn.microsoft.com/en-in/library/ms972976.aspx

    Thursday, April 18, 2013 5:18 AM
  • User541108374 posted

    Hi,

    hold the value using the controlstate

    Basically controlstate is the same like viewstate but doesn't get turned off when viewstate's disabled.

    Grz, Kris.

    Thursday, April 18, 2013 5:47 AM
  • User-1768369891 posted

    yes ,just about another way to manage viestate.

    Thursday, April 18, 2013 6:37 AM
  • User1449393024 posted
            If Not IsPostBack Then
                showList()
                If Not Request.QueryString("TaskID") Is Nothing Then
                    ViewState("TaskID") = Request.QueryString("TaskID")
                End If
                Dim Mode As String
                Mode = Request.QueryString("Mode")
            End If

    In this code when I trace the 4th line put a value in the viewstate.

    after clicking a button and refresh the page with another querystring and BEFORE the 4th line of code I see that viewstate is empty!

    what's the reason and how to resolve that?

    Thursday, April 18, 2013 7:43 AM
  • User541108374 posted

    Hi,

    refresh the page with another querystring and BEFORE the 4th line of code I see that viewstate is empty!

    Since this piece of code's in a check of IsPostback I'm wondering how you refresh the page. Normally on a postback you shouldn't get into that particular piece of code and your viewstate should be in order.

    I can only guess but likely you render out your page a first time, then change the querystring in the address bar and hit enter. Know that this isn't a postback but a new page request hence you've lost your viewstate.

     

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2013 9:22 AM