Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Managing state between postbacks to server
Message
General information
Forum:
ASP.NET
Category:
Web forms
Miscellaneous
Thread ID:
00640625
Message ID:
00641724
Views:
20
I think the idea of viewstate is to persist the value of controls for each round trip to the server. So, if you have a textbox and you set it's EnableViewState property then it value will be persisted on each trip to the server for you.

"An ASP.NET server control inherits a property named ViewState from Control that enables it to participate easily in state management. The type of ViewState is System.Web.UI.StateBag, which is a dictionary that stores name/value pairs. ViewState is persisted to a string variable by the ASP.NET page framework and sent to the client and back as a hidden variable. Upon postback, the page framework parses the input string from the hidden variable and populates the ViewState property of each control. If a control uses ViewState for property data instead of a private field, that property automatically will be persisted across round trips to the client. (If a property is not persisted in ViewState, it is good practice to return its default value on postback.)"

BOb


>I have a web form that has to post back to the server to refresh a combo box depending on what the user has selected in another combo box. I need the ability to save a couple of variables when this is done.
>
>According to the book I have (and I know I got this to work using Beta 2) - this is the code I need using 'ViewState' to get this to work:
>
>
>Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
>        'Put user code to initialize the page here
>        If Not IsPostBack Then
>            strTableName = "blah"         ' for testing purposes
>            strDatabaseName = "Blah,Blah" ' for testing purposes
>            Call FillDBCombo()
>            Call FillTableCombo()
>        Else
>            strTableName = ViewState("strTableName")
>            strDatabaseName = ViewState("strDatabaseName")
>        End If
>    End Sub
>
>    Sub Page_PreRender(ByVal Sender As Object, ByVal E As EventArgs)
>        ViewState("strTableName") = strTableName
>        ViewState("strDatabaseName") = strDatabaseName
>    End Sub
>
>
>
>However, when it does a postback, it resets the variables to empty strings. Any idea whay I'm missing here - or is there a better way to do this ?
>
>Thanks in advance,
>
>Al
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform