How to use View state and Hidden Fields in asp.net Website

By
The state of a web application helps you to store the run time changes that have been made to the asp.net web application.if you are not using states ,these changes are discarded and are not saved.In this tutorial i will implement view state and hidden field control in asp.net website.
View State:- View state stores page-specific information,when a page post back to the server.When a page is processed,the current state of the page and its controls is hashed into  a string and saved a s a hidden field,such a state of the page is called view state.In any asp.net application view state is maintained by default.
 >>   To disabled the view state on the web page directive:-                                                                       <@page Enableviewstate="false"%>                       
>>   To disable the view state at the coding time:-      
          Enableviewstate=false;                                                                                                    
               OR 
             Page.Eableviewstate=false;                                                 
There are some steps  to implement the view state  on the asp.net website.which are given below:-
Step1:- Open your visual studio -->File -->New-->Website-->ASP.NET Empty website-->ok--> open solution Explorer -->add a web form -->drag and drop Label and Button control the web form as shown below:-
home

Step2:- Now double click Enter Button-->write the following codes as shown below:-
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       // Page.EnableViewState = false;
        int i = Convert.ToInt32(ViewState["a"]) + 1;
        Label2.Text = i.ToString();
        ViewState["a"] = i;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("hiddenfield.aspx");
    }
}


Step3:-  Now Run the application(press F5)-->after that press Enter Button as shown below:-
output

Note:- View State store the data at the server side.

Hidden fields:- If the web page view state is completely disabled but still we want to maintain some information for the current page,we can use hidden fields control from the Toolbox.View State and hidden fields both are used to maintain information for the current page not other page.
There are some steps to implement the concepts of Hidden Field control .which are given below:-
Step1:- Open your visual studio -->File -->New-->Website-->ASP.NET Empty website-->ok--> open solution Explorer -->add a web form -->drag and drop Label and Button control the web form as shown below:-
home

Step2:- Now double click on Enter Button and write the following codes.which are given below:-
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class hiddenfield : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         Page.EnableViewState = false;
         HiddenField1.Value = "Welcome to our website:http://www.msdotnet.co.in";
         Label3.Text = HiddenField1.Value;
    }
    public void Button1_Click(object sender, EventArgs e)
    {
        HiddenField1.Value ="0";
        int i =0;
        i =(int.Parse(HiddenField1.Value)+1);
        Label2.Text = i.ToString();
        HiddenField1.Value =i.ToString();
    }
}

Step3:- Now Run the Application (press F5)-->after that Press Enter Button as shown below:-
hidden_output
Note:- Hidden fields is used for maintaining the current page information,when view state property is disable.

There are some similarities and difference between view state and hidden fields .
  1. Both maintains the current page information
  2. View state store the information at the server side.
  3. Hidden fields store the information at the client side.
  4. View state store the information in the 'object' form.
  5. Hidden fields store the information int the 'String' form.
  6. Hidden fields control are used ,when view state property of the current web is disabled.
  7. By default view state property of each web page is Enabled.
For More:-
  1. Cookie in asp.net website
  2. send mail fro asp.net website free
  3. create captcha without dll.
  4. Host asp.net website on server free
  5. Host asp.net web site on IIS Server
  6. overview of c#
  7. access-specifier in c#

To Get the Latest  Free Updates Subscribe
 Download whole application from below:-
                    Download 

2 comments:

  1. Hi Sir,
    I studied lots of time, The view state can be stored in client side.But in u r tutorial u wrote View state can be stored on serverside(NOTE).Please clarify me

    ReplyDelete
    Replies
    1. when page is reloaded,the server reads the values in the value attribute and restores them when page is send back to the client.

      Delete

Powered by Blogger.