Tuesday 17 September 2013

ViewState in Asp.Net.

In classic Asp ,when form is submitted , all form values are cleared. If you have filled lot of values in the form and submitted and due to some reason an error come from server. Then at that time you have to fill the form again with all the necessary data. When you click the button form is submitted and all the fields are clear automatically.
If you again fill the form with some minor changes then again you have to fill all the fields.
But in Asp.Net , when you submit the form with all values and form is post back then values are remain on the field. How this is possible. This is possible because of ViewState. ViewState store the value in hidden form in the web page. ViewState allows Asp.Net to repopulate form fields on each postback to server.
ViewSate values are not carried between pages. If you want to carries values between pages then you have to use cookies or session because cookies or session can be accessed from all your pages on your websites.
Let’s take example to see how ViewState works.

<%@ Page Language="C#" AutoEventWireup="true"   CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Submit & set Name"
            onclick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="Refresh"
            onclick="Button2_Click" /><br />
        Retrieve from ViewState :<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>   
    </form>
</body>
</html>

Run the form and see the out. On the label the value is Not Set.........
Fill the textbox and click the first button. Then you will the label value also change. Now click on second button and then you will see again label value has not change.
Clear the text value and click the second button. Then you will see the label contain the same value what you have enter in the textbox.

If you want to store more complex value or huge amount of data to use across the page or website then it’s better to use session or cookies.
ViewState is not secure for transferring the private data across the pages or website because it can be easily hack.

By default viewstate is enable in asp.net. You can enable or disable according to you requirement. You can enable or disable viewstate for whole page or some particular control.

If you want to enable viewsate for whole page then write the in design page just like as.

<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

For particular control :-


TextBox1.EnableViewState = true;

No comments:

Post a Comment

Note: only a member of this blog may post a comment.