ASP.NET: Viewstate in ASP.NET




It doesn't matter how many times you can click..: It wont increment beyond 1



We are going to click Control +F5 to run the application..


Any browser only understand HTML..It wont understand C#, Java, php.

Here you can see on this view code that clickcount will always be 0..It doesn't mater how many times you click



  The reason why it doesnt t increment is the stateless nature of this protocol.


 

Web Form only live for a moment
It's a GetRequest, it's not PostBack..

The application needs to generates HTML that the browser can understand..
After the HTML is send back to the client , the web form that was created is immediately destroy..





#1 doesn't know anything about web form #2

Every timewe click we are posting back to the web server
Web form is initialized to 0.It's a postback so the code wont be executed
Look at the example:




It immediately destroy the Web Form again.
The web server doesn't remember the state between request..


Now look at this different scenario...



Click is te variable , you can name it anything you want..

In the line ClickCount= (int)Viewstate["Click"] + 1  
There we are retrieving the value of Click, converting to an integer and we are adding 1 to that

At the end we are putting back that value at the ViewState of Clicks

So we create a webform2

1.-Page Load is executed  2.-It's not a post back so it wont execute that code

Look what happens when you click for the first time

Post back goes to the web server


Its not a post back so it wont execute that code
in #1  is null because we haven't store anything on that "Clicks"
so, line # 2 wont be executed
In line # 3 we are converting clickscount to  a string ..in this case clickscount count is 1
In Line #4 we are converting Clickcount to a Viewstate variable

Them it sends the html back to the web server and destroy that web form immediately but remember it store that ClicksCount and convert it into a Viewstate variable

The value of the TextBox  is stored in the viewstate  of "Clicks" so that get send back to the web server.
 ________________________________________________________________


Now if we click the button for the second time ,
 we check that format.....in line #1 Viewstate ("Clicks') =null ?  Yes, because previously we convert the Clickcount into a variable in line #4

So in line # 2 it replace the value of "Clicks"   and renew it with a Clicks variable, and it was 1 in previous request, converting that to and integer and increasing it by 1.
In line #4 it will store it in ViewState variable again..


So the next time you do it , the process repeats over and over every time we click.....



___________________________________________________

Now look at this: 


 Here we are getting the value from the text box , converting it to an integer and adding 1 to it..And storing it back at the textbox property of textbox 1



In Web Form 3  there is not ViewState.
That happens because in asp.net they use by default Viewstate to preserve the data across postback

________________________________________________________________



We will create a web form 4.We have the standard section on the toll box and the HTML section:





In the html we choose standard text on HTML control





If we write Test in both TextBox look what happens..


asp.net regain it state..HTML dont because asp.net uses ViewState and HTML dont after Postback ..








We check the source  of HTML

we see that runat=server isnt created yet...To convert to asp.net





We use runat=server


 After we do that , we go to design format and we click  with test we see both controls regain it  state





Viewstate date is encoded in 64 bit string








ASP.NET: Viewstate in ASP.NET ASP.NET: Viewstate in ASP.NET Reviewed by ohhhvictor on 10:41 AM Rating: 5

No comments:

Powered by Blogger.