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:-
Step2:- Now double click Enter Button-->write the following codes as shown below:-
04 | using System.Web.UI.WebControls; |
06 | public partial class _Default : System.Web.UI.Page |
08 | protected void Page_Load(object sender, EventArgs e) |
12 | protected void Button1_Click(object sender, EventArgs e) |
15 | int i = Convert.ToInt32(ViewState["a"]) + 1; |
16 | Label2.Text = i.ToString(); |
19 | protected void Button2_Click(object sender, EventArgs e) |
21 | Response.Redirect("hiddenfield.aspx"); |
Step3:- Now Run the application(press F5)-->after that press Enter Button as shown below:-
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:-
Step2:- Now double click on Enter Button and write the following codes.which are given below:-
04 | using System.Web.UI.WebControls; |
06 | public partial class hiddenfield : System.Web.UI.Page |
08 | protected void Page_Load(object sender, EventArgs e) |
10 | Page.EnableViewState = false; |
12 | Label3.Text = HiddenField1.Value; |
14 | public void Button1_Click(object sender, EventArgs e) |
16 | HiddenField1.Value ="0"; |
18 | i =(int.Parse(HiddenField1.Value)+1); |
19 | Label2.Text = i.ToString(); |
20 | HiddenField1.Value =i.ToString(); |
Step3:- Now Run the Application (press F5)-->after that Press Enter Button as shown below:-
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 .
- Both maintains the current page information
- View state store the information at the server side.
- Hidden fields store the information at the client side.
- View state store the information in the 'object' form.
- Hidden fields store the information int the 'String' form.
- Hidden fields control are used ,when view state property of the current web is disabled.
- By default view state property of each web page is Enabled.
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.