Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Managing state between postbacks to server
Message
From
06/04/2002 10:40:11
 
 
To
05/04/2002 23:00:37
General information
Forum:
ASP.NET
Category:
Web forms
Miscellaneous
Thread ID:
00640625
Message ID:
00641800
Views:
14
Bob,

Since the context of discussion here is state of a variable, let me clarify your comments about state of controls:

EnableViewState is true by default for both the page and for all controls on it, so ASP.NET will automatically maintain state of controls between trips to the server unless you turn it off.

You can set EnableViewState to false on individual controls or can use this directive to turn it off for the entire page:
<%@ Page EnableViewState="false" %>
>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
David Stevenson, MCSD, 2-time VFP MVP / St. Petersburg, FL USA / david@topstrategies.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform