--VIEW STATE IN ASP.NET
--ASP.NET WORKS ON HTTP PROTOCOL. HTTP PROTOCOL IS STATELESS PROTOCOL, MEANING IT DOESNOT RETAIN STATE BETWEEN USER REQUEST.
--WEB FORM LIVES BARELY FOR A MOMENT.
--AN INSTANCE OF REQUESTED WEBFORM IS CREATED
--EVENTS PROCESSED
--GENERATES THE HTML AND AND POSTED TO THE CLIENT
--THE WEBFORM IS IMMEDIATELY DESTROYED
==============================================================
code behind :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace VIEWSTATEINDEPTH
{
public partial class _Default : System.Web.UI.Page
{
int count = 100;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtcounter.Text = "100";
}
}
protected void txtbutton_Click(object sender, EventArgs e)
{
count = count + 1;
txtcounter.Text = count.ToString();
}
}
}
=====================aspx page=====================================
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="viewstate.aspx.cs" Inherits="VIEWSTATEINDEPTH._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="txtcounter" runat="server"></asp:TextBox>
<asp:Button ID="txtbutton" runat="server" Text="Click Here"
onclick="txtbutton_Click" />
</div>
</form>
</body>
</html>
Screenshot of soultion explorer
--let me change that to _webform1 as it will be more clear when i explain all the things in detail. Now once you change here. You got to change in aspx page too. Otherwise you will get error.
--ASP.NET WORKS ON HTTP PROTOCOL. HTTP PROTOCOL IS STATELESS PROTOCOL, MEANING IT DOESNOT RETAIN STATE BETWEEN USER REQUEST.
--WEB FORM LIVES BARELY FOR A MOMENT.
--AN INSTANCE OF REQUESTED WEBFORM IS CREATED
--EVENTS PROCESSED
--GENERATES THE HTML AND AND POSTED TO THE CLIENT
--THE WEBFORM IS IMMEDIATELY DESTROYED
==============================================================
code behind :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace VIEWSTATEINDEPTH
{
public partial class _Default : System.Web.UI.Page
{
int count = 100;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtcounter.Text = "100";
}
}
protected void txtbutton_Click(object sender, EventArgs e)
{
count = count + 1;
txtcounter.Text = count.ToString();
}
}
}
=====================aspx page=====================================
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="viewstate.aspx.cs" Inherits="VIEWSTATEINDEPTH._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="txtcounter" runat="server"></asp:TextBox>
<asp:Button ID="txtbutton" runat="server" Text="Click Here"
onclick="txtbutton_Click" />
</div>
</form>
</body>
</html>
=================================================================
screenshot of aspx page
Screenshot of soultion explorer
--let me change that to _webform1 as it will be more clear when i explain all the things in detail. Now once you change here. You got to change in aspx page too. Otherwise you will get error.
Now when you click the button. The textbox value at the start is 100. It gets incremented by 1[101]. Then if you click again it remains as 101....Even if you try so many times it will be 101. Why ?
Initial get request-->100
button hit i.e post back[posting the page back]. -->increament to 101.
The reason why it doesn't increment because of stateless nature of http protocol.
Webforms live only for a moment.
When page loads-->Request goes to web server. [so it is a get request.]. We are not posting the webform back to the server. instead we are getting the webform.
Webform is technically a class. The instance of the webform is created. Now this instance how its created.
When we build the solution, then the entire source code of the application will be compiled into
intermediate language. Since this is a web application the application assembly is generated which is nothing but a dll.
Since the project name is VIEWSTATEINDEPTH we will have an assembly for this web application with the same name as dll.
--dll.
So when you request the webform1 then this assembly gets loaded into the memory which creates instance of the requested webform which in our case is webform1. And what ever controls are there they are also technically classes.
So as the page loads, instance of the webform will be created and also instance of textbox and button which are present in the form , they will be also created.
So the below screenshot tells you that button,textbox and aswell as webform is a class.
Get request
Goes into post back. Initial post back will be false. condition is true. So goes into that if block. sets that text to 100.
if (!IsPostBack)
{
txtcounter.Text = "100";
}
Then it doesn't go to button click event. Then it generates the html.We know that client for our web application is web browser. And web browser can understand only html.
Once it generates the html...it sends that html to the client who requested that. Now what happens to all the instances. It immediately gets destroyed[webform1 , button , textbox instances]
Hit the button --first time
When yo click the button. Then it is POST BACK. post the webform to the server.
Again the instance of webform1 is created. Initializes the count to 100.
It doesn't go to the ispostback if loop. Then in button click event again it becomes 101.
Hit the button --second time
Again hit the button. Then it is POST BACK.
Again the instance of webform1 is created. Initializes the count to 100.
It doesn't go to the ispostback if loop. Then in button click event again it becomes 101.
So the same thing.
if (!IsPostBack)
txtcounter.Text = "100";
}
protected void txtbutton_Click(object sender, EventArgs e)
Once it generates the html...it sends that html to the client who requested that. Now what happens to all the instances. It immediately gets destroyed[webform1 , button , textbox instances]
Hit the button --first time
When yo click the button. Then it is POST BACK. post the webform to the server.
Again the instance of webform1 is created. Initializes the count to 100.
It doesn't go to the ispostback if loop. Then in button click event again it becomes 101.
Hit the button --second time
Again hit the button. Then it is POST BACK.
Again the instance of webform1 is created. Initializes the count to 100.
It doesn't go to the ispostback if loop. Then in button click event again it becomes 101.
So the same thing.
int count = 100;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtcounter.Text = "100";
}
}
protected void txtbutton_Click(object sender, EventArgs e)
{
count = count + 1;
txtcounter.Text = count.ToString();
}
}
}
---------------------------Now This works pretty cool-----------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace VIEWSTATEINDEPTH
{
public partial class _webform1 : System.Web.UI.Page
{
int count = 100;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtcounter.Text = "100";
}
}
protected void txtbutton_Click(object sender, EventArgs e)
{
if (ViewState["click"] != null)
{
count = (int)ViewState["click"] + 1;
}
txtcounter.Text = count.ToString();
ViewState["click"] = count;
}
}
}
important
you can click on view source and see that the view state is stored in hidden field. And you can take that base 64 encoded and decode it using any decoding site. So obviously its not safe. So you must be careful that the password should not be stored as view state.
--View state is stored in hidden fields in client side.
--view state is base 64 encoded and so not good for critical information.
---------------------------Now This works pretty cool-----------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace VIEWSTATEINDEPTH
{
public partial class _webform1 : System.Web.UI.Page
{
int count = 100;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtcounter.Text = "100";
}
}
protected void txtbutton_Click(object sender, EventArgs e)
{
if (ViewState["click"] != null)
{
count = (int)ViewState["click"] + 1;
}
txtcounter.Text = count.ToString();
ViewState["click"] = count;
}
}
}
----------------------------------------------
View Source-->_view state-->It is storing the view state of clicks in base 64 encoded format. hidden view state field gets transferred between client and server..between every response and request.
hit the button next time.
the value is stored in view state of clicks variable. that gets sent to the server.
There are several levels of initialization like below
1)page initialization2)page load
view state recognization occurs in page initilazation.
So view state variable is used to preserve the data between requests.The view state travels with every request and response between the client and web server.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace VIEWSTATEINDEPTH
{
public partial class _webform1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtcounter.Text = "100";
}
}
protected void txtbutton_Click(object sender, EventArgs e)
{
int count =Convert.ToInt32(txtcounter.Text) + 1;
txtcounter.Text = count.ToString();
}
}
}
--how its working...how the text value retains its value during post back.
Behind the scenes all the server controls in asp.net retain their value during their post back.
Textbox is a server control that uses view state internally to preserve data across postback. Because webforms have very short lifetime,
--Difference between Html control and Server control
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="viewstate.aspx.cs" Inherits="VIEWSTATEINDEPTH._webform1" %>
<!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>
server control
<asp:TextBox ID="txtcounter" runat="server"></asp:TextBox>
<br />
html control
<input id="Text1" type="text" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
After you hit the button
Asp.net server controls retain its state because it uses view state to retain its state.
To convert html controls to server control . Make use of runat="server"
important
you can click on view source and see that the view state is stored in hidden field. And you can take that base 64 encoded and decode it using any decoding site. So obviously its not safe. So you must be careful that the password should not be stored as view state.
--View state is stored in hidden fields in client side.
--view state is base 64 encoded and so not good for critical information.
No comments:
Post a Comment