13

What is the difference when using

  • Hidden field vs View state?
  • When to use each one ?
  • Which one more secure?
  • Which is better in performance?
  • what are the alternatives?
Anirudha
  • 32,393
  • 7
  • 68
  • 89
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392

3 Answers3

8

ViewState is stored in a hidden field and it contains information about the entire page. It can also be encrypted. Because the view state is always sent to the codebehind when performing Postbacks it is very practical as you always get the values. The drawback is that it can get really large if you start putting much information inside it and performance could start to suffer. For example in some AJAX requests you want to only send some small information to the server and if you used UpdatePanels the entire ViewState will be sent and it will contain information that is not necessary.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • yeah ,, u mean that using viewstate with heavy use of AJAX is not a performance wise? – Anyname Donotcare Jan 23 '11 at 09:44
  • 1
    @just_name, it depends how you are doing AJAX. If you are using UpdatePanels, then no, it is not wise, if you are using some other framework such as jQuery you have full control of what is being sent to the server. – Darin Dimitrov Jan 23 '11 at 09:46
3

A hidden field can be viewed in a pages HTML source whereas ViewState is, to say the least, obfuscated and depending on your .net version, can be encrypted to varying degrees.

asp.net viewstate encryption

Hidden field will be better in performance but provides no security and if the post data can be manipulated, be a lot easier to change the ViewState.

Session variables are a good alternative to these.

Community
  • 1
  • 1
PMC
  • 4,698
  • 3
  • 37
  • 57
  • 2
    +1 For example if you want client side scripts to be able to easily access the value, use a hidden field. – Lisa Jul 19 '11 at 07:42
-1

ViewState internally uses hidden field. It is managed by ASP.NET Engine and is encrypted by default.

On the other hand, with hidden field control, you get to manage what is stored in it. By default, it's not encrypted.

Performance wise, both are same. But, I feel ViewState is more secure. ASP.NET maintains its hash to prevent/identify any tampering with it client side.

You can also use session to store data.

Yojimbo
  • 23,288
  • 5
  • 44
  • 48
decyclone
  • 30,394
  • 6
  • 63
  • 80