ViewState

What is ViewState?

In web applications prepared with Asp.Net, when it is necessary to process the information on the page, the page is sent to the server and comes back again. That is called PostBack.

During PostBack, that is, when the page is going to and from the server, it will be a big problem to delete the data entered by the user in the controls on the page.

For example, let the user add as many elements as they want to a DropDownList control. Every time he adds a new element, the page will go to the server, when he comes back, the last element he added will stay in the DropDownList, while the element he added before will be deleted.

This problem is eliminated by the ViewState object. When sending a page to the server, information about the controls on the page (content, width, color, etc.) is added to the ViewState object and sent. While the page is being created on the server, the properties of the controls are set according to the information in the ViewState and sent to the client.

When we give the "View source" command on the web page, we can see this object whose content is encrypted.

EnableViewState Property

In some cases, it may not be desirable to move the data in the controls to the server. In this case, the EnableViewState property of the corresponding control is set to False.

To illustrate with an example, put a DropDownList, a TextBox and a Button on the web page and write the following in the code page.

protected void Button1_Click(object sender, EventArgs e)
    {
         DropDownList1.Items.Add(TextBox1.Text);
    }

When you run the program, every time we press the button, it will add the text entered in the box into the dropdownlist. Since the enableViewState property of the DropDownList1 control is True, the information inside the page will not be lost during PostBack.

Then, if you run the application again by setting the enableViewState property of the DropDownList1 control to False, you will see that each button press adds a new element, but the previous ones are deleted because they cannot be moved.

Note: Even if we set the enableViewState property of the TextBox objects to False, the information in it is moved.

Difference between ViewState object and QueryString: With QueryString we can move data between different pages. ViewState, on the other hand, is deleted when switching to a different page, it is valid only for that page.

Adding Data Into The ViewState

If we want to add data to the ViewState, we can do this in two ways.

ViewState.Add("parameter-name", "value"); 

or

ViewState["parameter-name"]=value;

can be made.

Information in the ViewState can be read in the same way:

 string a = ViewState["a"].ToString();

* The type of data stored in ViewState is Object.

** Information stored with ViewState will only be valid for that page. It disappears as soon as the page changes.

What is viewstate, how to use viewstate, adding data into viewstate, asp.net viewstate operations and examples, what is the use of enableviewstate feature, viewstate and querystring difference

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 712 times.

Online Users: 1146



using-viewstate