View State: ASP.Net technology provides View State feature to the web forms. View State is used to remember controls state when page is posted back to server. Asp.Net uses View State to track the values in the Controls. You can add custom values to the view state. The ViewState property provides a dictionary object for retaining values between multiple requests for the same page. This is the default method that the page uses to preserve page and control property values between round trips.
ASP.Net stores view state on client site in hidden field __ViewState in encrypted form. When the page is processed, the current state of the page and controls is hashed into a string and saved in the page as a hidden field, for multiple hidden fields if the amount of data stored in the ViewState property exceeds the specified value in the MaxPageStateFieldLength property. When the page is posted back to the server, the page parses the view-state string at page initialization and restores property information in the page.
Code Semple:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(ViewState["NameOfUser"] != null)
NameLabel.Text = ViewState["NameOfUser"].ToString();
else
NameLabel.Text = "Not set yet...";
}
protected void SubmitForm_Click(object sender, EventArgs e)
{
ViewState["NameOfUser"] = NameField.Text;
NameLabel.Text = NameField.Text;
}
}
View state can be used for storing any type of data because it is of object type. View state provides good performance for application without giving any load to server. You can enable and disable view state behavior of page and its control by specifying 'enableViewState' property to true and false. You can also store custom information in the view state as described in above code sample. This information can be used in round trips to the web server.
No comments:
Post a Comment