Search
Sponsor
deccansoft ad
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Articles Author
Tue, 23-Sep-2014 Authors
All Time Authors
Sourav.Kayal
39750
Sheonarayan
38050
Niladri.Biswas
33350

Latest members | More ...


(Statistics delayed by 5 minutes)
Ads
 Article

Common misconception regarding ViewState and TextBox

Virendradugar
Posted by under ASP.NET category on for Beginner level | Views : 13268 red flag
If you found plagiarised (copied) or inappropriate content,
please let us know the original source along with your correct email id (to communicate) for further action.
Rating: 5 out of 5
1 vote(s)
This article explains a common myth regarding the Viewstate and textbox.

Introduction
Web is Stateless.That means it does not maintain states between two or more postbacks, all your controls values is lost. For web server, Every request is a new request. But the technology with which we work provides various techniques, mechanism to maintain state. As for example, ASP.NET provides session variables to maintain session of user between postbacks. ASP.NET also provides a mechanism to maintain the values of all the controls placed on the page through "ViewState". You can see the viewstate of any page by it's view source. (Right Click on Page -> Select View Source).
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE0OTIwMzUyNTUPZBYCZg9kFgICAw9kFgQCAQ9kFgJmD2QWAmYPZBYCAgEPDxYCHgR1JUZGQCAw9kFgQCAQ8PFgIfAAU"/>
More data, more controls leads to large view state which can drastically hit you for performance. As with every request, this data goes to server and get's updated and sent back to client. So the response size becomes more and it's hits the performance. Developers use various techniques to get rid of this problem by storing view state in database or session.

Now let's get to the point.

There is common myth that most of the asp.net developers believe that ViewState is responsible for maintaining values of controls like textbox. But that's not true. :) 

EnableViewState Property

There is a property in ASP.NET called "EnableViewState". This property specifies that whether you want to maintain Viewstate for a control or not. You can also specify at page level. When specified at page level, ViewState will not be maintained for all the controls of the page. By Default it is true.

Gotcha

Before some days I was just exploring that How EnableviewState works. I noticed that it does not work with the textbox. Let's take an example:

Put a text box with EnableViewState= “false” and a button on the page.

<asp:textbox id="TextBox1" EnableViewState="False" runat="server"/>
<asp:button id="Button1" onclick="Button1_Click" runat="server" text="Button"/>
Enter some value in the text box and Click the button, a postback happens. Request goes to server and response is sent back to the client. You must be expecting that value that you have entered in Textbox will be lost and it will be empty after postback. Look at the textbox now. Oops!!! Text box is still maintaining its value. Weird.. is not it? Though You have explicitly told that ASP.NET that don't maintain textbox's viewstate but it does.

Well, Textboxes do not use ViewState to maintain/hold their values between postbacks. Values are maintained in HTTP Post headers. But what could be the reason? 

Why ViewState does not maintain values?

Controls like Textbox, is inherired from IPostBackDataHandler interface. After Page_init() events, LoadViewState event is executed which is responsible for load view state for the controls. Page class loads the ViewState from the hidden field __ViewState. After LoadViewState event, LoadPostBackData event is executed, in which values of those controls (which are inherited from IPostBackDataHandler  interface) is assigned from HTTP Post header. Server simply takes the value from the header and add an attribute to the textbox "value" which is set to the value previously entered by the user.

References

ViewState an animal

IPostBackDataHandler

Conclusion

We have seen that controls which are inherited from IPostBackDataHandler  interface will maintain it's values even if viewstate is disabled.

Enjoy..
Page copy protected against web site content infringement by Copyscape
About the Author

Virendradugar

Full Name: Virendra Dugar
Member Level: Silver
Member Status: Member,MVP
Member Since: 8/11/2009 4:14:05 AM
Country: India

http://jquerybyexample.blogspot.com

Virendra Dugar is experienced Senior Software Developer with over 5 years of hands-on experience working with Microsoft .NET technology (ASP.NET, C#, VB.NET,SQL Server). He is always keen to learn new technology. He holds a Master's Degree in Computer Application & Information technology from Gujarat University in india.In free time, he loves to listen music, read books, play games and do blogging etc. Visit his blogs : http://jquerybyexample.blogspot.com
Login to vote for this post.
Found interesting? Add this to:


Comments or Responses

Posted by: SurajRane on: 4/15/2010

Nice viru!!keep it. I was searching why TextBox maintains its value. Now got it.
Thanks.

Login to post response

Comment using Facebook(Author doesn't get notification)