Monday, May 28, 2012

Interview questions of viewstate

Different types of state management?

There are two different types of state management:
  1. Client Side State Management
    • View State
    • Hidden Field
    • Cookies
    • Control State
  2. Server Side State Management
    • Session
    • Application Object
    • Caching
    • Database
What is View State ?
View state is Client Side StateManagement ,
View State will stores the page value during the post back to the server.
It will stores the page value in Base64 Encoded Stiring which is suitable for HTTP Protocols
View state will allows you to store the value of form controls in every post back to the server.
View state will store the value in Hidden fields .So we need to use seriliaztion .


Where is view state stored?

View state will store the value of page control in a string .
We can store the view state for a control as below syntax:
ViewState["Variable"]=Textbox.Text;
We can retreive the view state value as below syntax :
string Test=ViewState["Variable"];


How can we Enable and Disable the view state 
We can able to Enable and Disable the view state in Page Level as will as Application Level

Page Level :
EnavbleViewState=”False”


  

On doing so we can able to Enable/Disable the view state in Page Level
We can Enable view state on writing code in cs file as below
TextBox1.EnableViewState =true;


Application Level :
We can able to Enable/Disable the view state in Application Level as below :
in web.config file :
<system.web>
<pages enableViewState="true"> </pages>



How we can Trace the view state Information ?
We can able to Trace the Viewstate Information by just Enabling the Trace option
In page attributes Trace=”True”

    After Tracing How we can view the view state Information 
1.Run the Application where you strored the view state 
2.When the Aspx page is running in browser ,Right click and select “View Source “
you can able to see the view state Information as below Image

                     

How to Provied Security for View state ?
We can approch the functionality as 2 steps.
1.By Just Adding a simple attributes in page level – EnableviewstateMac=”True”
2.We can encript the view state data by adding the attribute in page level- ViewStateEncryptionMode="Always/Auto/Never" 

EnableviewstateMac=”True”
ViewStateEncryptionMode="Always"


Advantages of view state ?
1.View state is very easy to implement , 
2.We can able to store any kind of value in string
No server resource is required for this because it is client side Management.


Disadvantage of View state ?
1.Date was not that much secure because it is just Base64 Encoded string , it is very easy to hack .
2.When the view state data is in large amount it may causes application performance issue.

1 comment: