How to use viewstate

/
0 Comments

View state is a technique that automatically persists programmatic changes to the Web controls on a page. By default, this state is serialized into a base-64 encoded string and included as a hidden input field in the Web Form. On postback, this state information is returned to the server as part of the POST request, at which point the server can deserialize it and reapply the persisted state to the controls in the control hierarchy.
Viewstate is used to maintain or retain values on postback. It helps in preserving a page. Viewstate is internally maintained as a hidden field in encrypted form along with a key.

Assign value into ViewState

ViewState["User"] = "Some Value";

It will assign the string value into viewstate with key name "User".

Retrieve value from ViewState

string user = ViewState["User"].ToString();

It will retrieve the value from viewstate with key name "User".

Remove Particular Key from ViewState

ViewState.Remove("User");

It will remove the value from viewstate with key name "User".

Assign custom object into ViewState

Assign custom object into ViewState

It will assign the employee class into viewstate with key name "EmployeeInfo".

Retrieve/Convert custom object from viewstate

Retrieve custom object from viewstate

It will retrieve the employee class from viewstate with key name "EmployeeInfo".

Using viewstate for get/set like property

public string UserName
{
get { return ViewState["username"] as string; }
set { ViewState["username"] = value; }
}

It will assign and retrieve the value with key "username" and it will work as property or member in the page

Enable ViewState by Page Level

Enable ViewState by Page Level

It will enable the viewstate for this page and it is enabled by default.

Disable ViewState by Page Level

Disable ViewState by Page Level

It will disable the viewstate for this page and it is enabled by default.

Enable ViewState by control Level

enable ViewState by control Level

It will enable the viewstate for this control and it is enabled by default.

Disable ViewState by control Level

disable ViewState by control Level

It will disable the viewstate for this control and it is enabled by default.


You may also like

No comments:

Powered by Blogger.