Reputation: 30303
In a web application, I create a viewstate to maintain the value of variable within the page only, can I have anything like viewstate in WinForms, to maintain the values within the forms?
Upvotes: 0
Views: 1077
Reputation: 5899
In windows foms, there is no need to save values in viewstate or sessions. See the link
But if you want to maintain values between asp.net forms, you can use Sessions
Upvotes: 0
Reputation: 12966
ViewState exists to get around the fact that websites use HTTP, a stateless protocol. In order to give the illusion of state, a load of encoded data is sent to the client, and back to the server on every POST. This is ViewState.
Windows Forms programs have state, it's a process that's running for as long as the program is open. So you can just use member variables in your form classes.
Upvotes: 1
Reputation: 927
As you applicaition is stateful, you can store data as you like - in-memory collections, fields of classes, files, xml, db. Viewstate intended to store data between postbacks, and there is no postbacks in winforms app.
Upvotes: 0
Reputation: 1038720
You don't really need viewstate in Windows Forms because you can store state for example in your main window class using public properties. And as long as your main form lives (which normally is the lifetime of the application) the state will be preserved. If you need to persist the state after the application closes you could store it in files or database.
ViewState is required in ASP.NET because the ASP.NET Form is destroyed after each request and you cannot store instance variables in it. You could use static members but then you get problems as static members are shared among all users of this ASP.NET application.
Upvotes: 4