C++ C++ C# C# ASP.NET Security ASP.NET Security ASM ASM Скачать Скачать Поиск Поиск Хостинг Хостинг  
  Программа для работы с LPT портом...
Язык: .NET — ©Alexey...
  "ASP.NET Atlas" – AJAX в исполнении Micro...
Язык: .NET — ©legigor@mail.ru...
  "Невытесняющая" Многопоточность...
Язык: C/C++ — ©...
  Update World C++: Сборник GPL QT исходников
  Весь сайт целиком можно загрузить по ссылкам из раздела Скачать
Дебетовая карта Home Credit [CPS] RU

 Myth Regarding ViewState in ASP.NET / Viewstate / ASP. NET

Introduction

Most ASP.NET developers think that the ASP.NET ViewState is responsible for holding the values of controls such as TextBoxes so that they are retained even after postback. But this is not the case.

I'll explain with an example. You can download the project files from the link above the article (you can set the Virtual Directory by the name of ViewState) and run the application. Let's start with a web project in C#.

Open a new ASP.NET Web Application and on the WebForm place the following controls accordingly.

Place one web server TextBox control, one HTML server text box control (normal HTML input box with runat=server), a normal HTML input control, and a web server Label control. Name the four controls as:

  • txtWebServerTest
  • txtHtmlServerTest
  • txtHtmlTest
  • lblTest

Set the Text/Value property to “Initial Text” for the above three TextBoxes, and set the Text property of the Label control to “Initial Label Value”.

Set the EnableViewState property to False for the first and the last control.

Beneath these controls, place two Button controls and set their text as “Change Label’s Text” and “Postback to Server”. On the button click event handler of the first button, write the following code:

private void btnChangeLabel_Click(object sender, System.EventArgs e)
{
    lblTest.Text = "Label's Text Changed";
}

There is no code on the second button’s Click event. It just submits the page to the server.

Now run this application. You can see the initial texts in the controls as you have set.

Now change the text in all TextBoxes and set it to “Changed Text”. Now click the Post to Server button. What happens is that the first two textboxes retain their values, in spite of the ViewState property being set to false. But the last textbox, which is a simple HTML input control, lost the modified value when the page reloaded.

Most developers would have expected all three textbox controls to lose their modified values (“changed Text”) and after page re-loading they expect “Initial Value” being written on all textboxes as we had disabled the ViewState.

The reason for this behavior is that ViewState is not responsible for storing the modified values for controls such as TextBoxes, dropdowns, CheckBoxList etc., i.e., those controls which inherit from the IPostBackDataHandler interface. After Page_Init(), there is an event known as LoadViewState, in which the Page class loads values from the hidden __VIEWSTATE from the field for those controls (e.g. Label) whose ViewState is enabled. Then LoadPostBackData event fires, in which the Page class loads the values of those controls which inherit from IPostBackDataHandler interface, (e.g. TextBox) from HTTP POST headers. Also, HTML Input button class is derived from IPostBackEventHandler interface, which is why its state is also persisted in the same way as that of the TextBox control.

Now, again start the application and this time click the “Change Label’s Text” button. When the page reloads, you will see that the programmatic change (made by our code in the event handler of the button’s Click event) was lost, i.e. we don’t see the label’s text changed to “Label's Text Changed”. Instead we see the initial value of the label again. This is because Label control does not inherit from IPostBackDataHandler interface. So ViewState is responsible for persisting its value across postbacks, and since it has been disabled, the label loses its value after clicking the “Change Label’s Text” button.

Now enable the ViewState for the Label control and you can see the modified value (“Label's Text Changed”) after clicking the same button.

So we conclude that controls which inherit from IPostBackDataHandler/ IPostBackEventHandler interfaces will retain their values even if ViewState has been disabled as their values are stored in HTTP POST headers.




Дебетовая карта Home Credit [CPS] RU