Asp.Net Page render
html to browser . Each time a new page is requested , html and class
created newly .When a page is posted to the server , all information enter in the
browser interface lost. For example, if you have enter text in a textbox and the page is posted to the server , after the page make round trip , textbox text will lost data. To avoid this, Asp.Net introduce some new functionality, call State Management .There are several techniques to manage State. ASP.NET allow State Management Client and Server , both ways.
Here is a list of Client side and Server side State Management techniques as follows.
Client sideHere is a list of Client side and Server side State Management techniques as follows.
- Hidden Field
- View State
- Cookies
- Control State
- Query Strings
Server side
- Session
- Application
Hidden Field : Hidden field is Asp.Net controls which are not visible in browser. Hidden field are fully Programmable and Manageable. Hidden field can retain its value, after page have made round trip. Each hidden field can store one value . Asp.Net page can hold multiple hidden field.When a page is posted to the server , hidden field also post with other control collection . After the postback , hidden field retain its information .This functionality is happen on HTTP POST the server.
ClientID | Gets the control ID for HTML markup that is generated by Server |
---|---|
EnableTheming | Gets or sets a value indicating whether themes applicable or not |
EnableViewState | Gets or sets value indicating server control have view state |
ID | string to identify a the server control |
Value | Gets or sets the value of the hidden field |
Visible | Gets or sets value that control will be visible in browser or not |
Example
The follwoing example show , hidden fields 'HiddenField1' is assigned a string in
button click event and posted to the server ,after postback , value is retain by
'HiddenField1'
Aspx Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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:Button
ID="Button1"
runat="server"
Text="Button"
onclick="Button1_Click"
/>
</div>
<asp:HiddenField
ID="HiddenField1"
runat="server"
/>
</form>
</body>
</html>
C# Code
using System;
using
System.Web.UI.WebControls;
public
partial
class
Default3
: System.Web.UI.Page
{
protected
void
Page_Load(object
sender, EventArgs
e)
{
if
(HiddenField1.Value != null)
{
string
myValue = HiddenField1.Value.ToString();
Response.Write(myValue);
}
}
protected
void
Button1_Click(object
sender, EventArgs
e)
{
HiddenField1.Value
= "Asp.Net
State Management Hidden Field Example";
}
}
Asp.Net State Management Hidden Field Example
View State : ViewState is a dictionary object , ViewState retain value in key-value format. Each Asp.Net page may content several ViewState. Whens a page is posted with HTTP post, and load again, ViewState retain it information unchanged .When is page is posted to the server, the entire ViewState is hashed to a long string. This hash value is assigned to a hidden field , in the page initialization stage, this hash value again assigned to the view state.Thus a page restore it's ViewState collection .The following value can be stored in ViewState.
- Strings
- Integers
- Boolean values
- Array objects
- ArrayList
- Hash tables
- entire application : To enable or disable ViewState for entire application , you need to write invoice configuration file. Below is the example
xml version="1.0"?><configuration><system.web><pages enableViewState="false"/></system.web></configuration>
- page level : You can also enable or disable ViewState at page level. At the top of page the code is written, here is a example.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" EnableViewState="false" %> - control : ViewState can be applicable for single control also. You can enable or disable View State control to control.
<asp:TextBox
ID="TextBox1"
runat="server"
ViewStateMode="Enabled"
></asp:TextBox>
StateBag manage ViewState object. Here is some commonly used attribute of a StateBag.
Type | Name | What is ? |
---|---|---|
Property | Item(String) | Gets or sets the value of an item stored in the StateBag object. |
Property | Count | How many item is there in the StateBag object. |
Property | keys | Returns the collection of keys in the StateBag object. |
Property | value | Return the collection of ViewState value. |
Method | Add(name,value) | Add new ViewState Item in StateBag object. |
Method | Clear | Clear ViewState collcetion from StateBag object. |
Method | Remove | Remove ViewState collcetion from StateBag object. |
The follwoing example show , ViewState ,'StateValue' is assigned a string in
button click event and posted to the server ,after postback , value is retain by
ViewState ,'StateValue'
Aspx Page
<%@
Page
Language="C#"
AutoEventWireup="true"
CodeFile="Default3.aspx.cs"
Inherits="Default3"
%>
<!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:Button
ID="Button1"
runat="server"
Text="Button"
onclick="Button1_Click"
/>
</div>
</form>
</body>
</html>
C# Code
using
System;
using
System.Web.UI.WebControls;
public
partial
class
Default3
: System.Web.UI.Page
{
protected
void
Page_Load(object
sender, EventArgs
e)
{
if
(ViewState["StateValue"]
!= null)
{
string
myValue = ViewState["StateValue"].ToString();
Response.Write(myValue);
}
}
protected
void
Button1_Click(object
sender, EventArgs
e)
{
ViewState["StateValue"]
= "Asp.Net
State Management View State Example";
}
}
Cookies :Cookies are small text file stored in client computer or browser memory. The size of a Cookie is limited and Cookies are not permanent. Cookie has a expiration date of time. A client send some cookie information to the server and server also capable to read the cookies. Cookie Store information small information. A particular site can use multiple cookies. A client can read a control value and assign it to the cookie, the page is then posted to the server, when page is rendered again , server read the cookies assign the value to the respective controls.
Example
The follwoing example show , cookie ,'StateValue' is assigned a string in
button click event and posted to the server ,after postback , value is retain by
cookie ,'StateValue'
Aspx Page
<%@
Page
Language="C#"
AutoEventWireup="true"
CodeFile="Default3.aspx.cs"
Inherits="Default3"
%>
<!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:Button
ID="Button1"
runat="server"
Text="Button"
onclick="Button1_Click"
/>
</div>
</form>
</body>
</html>
C# Code
using
System;
using
System.Web.UI.WebControls;
using
System.Web;
public
partial
class
Default3
: System.Web.UI.Page
{
protected
void
Page_Load(object
sender, EventArgs
e)
{
if
(Request.Cookies["StateValue"]
!= null)
{
string
myValue = Request.Cookies["StateValue"].Value.ToString();
Response.Write(myValue);
}
}
protected
void
Button1_Click(object
sender, EventArgs
e)
{
HttpCookie
myCookie = new
HttpCookie("StateValue");
myCookie.Value
= "Asp.Net
State Management Cookie Example";
myCookie.Expires
= DateTime.Now.AddSeconds(10);
Response.Cookies.Add(myCookie);
}
}
Output :
Asp.Net
State Management Cookie ExampleA cookie size limits of 4 KB . Large data cannot be stored in cookie. An user can disable cookie from his Browser and cookie are easily tamperable . There are two types of cookies
persistent cookie and not persistent cookie.
Persistent cookies : Persistent cookies has expiry date and time.
HttpCookie
myCookie = new
HttpCookie("myCookie");
myCookie.Value
= "Persistance
Cookie Example";
myCookie.Expires
= DateTime.Now.AddSeconds(100);
Response.Cookies.Add(myCookie);
Non persistent cookie : It exist still the user using browser and do not have any input up expiry date.
Response.Cookies["myCookie"].Value
= "Non
Persistance Cookie Example";
You need to set ViewState property in the web.Config file, EnableViewState to true or false.
Here is the example of Web.Config file
xml
version="1.0"?>
<configuration>
<system.web>
<pages
enableViewState="false"
/>
</system.web>
</configuration>
Control State :Not yet updated
Query Strings : The query string is one of the technique to pass information from one page to another page. Query string is some additional string with URL . Query string starts with (?) . A query string has two part, field and value . Multiple field can be added with the symbol (&). Query string cannot handle long data. Query string is not safe, it can be easily tamper. To retrieve query the following Syntax is used.
Request.QueryString(variable)[(index)|.Count]
Source.aspx
<%@
Page
Language="C#"
AutoEventWireup="true"
CodeFile="Source.aspx.cs"
Inherits="Source"
%>
<!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">
<asp:TextBox
ID="TextBox1"
runat="server"></asp:TextBox>
<asp:TextBox
ID="TextBox2"
runat="server"></asp:TextBox>
<div>
<asp:Button
ID="Button1"
runat="server"
Text="Button"
onclick="Button1_Click"
/>
</div>
</form>
</body>
</html>
Source.aspx.cs
using
System;
using
System.Web.UI.WebControls;
using
System.Web;
public
partial
class
Source
: System.Web.UI.Page
{
protected
void
Page_Load(object
sender, EventArgs
e)
{
}
protected
void
Button1_Click(object
sender, EventArgs
e)
{
Response.Redirect("Destination.aspx?Parameter1="
+ TextBox1.Text + "&Parameter2="
+ TextBox2.Text);
}
}
Destination.aspx
<%@
Page
Language="C#"
AutoEventWireup="true"
CodeFile="Destination.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>
</div>
</form>
</body>
</html>
Destination.aspx.cs
using
System;
using
System.Collections.Generic;
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)
{
string
param1 = Request.QueryString["Parameter1"];
string
param2 = Request.QueryString["Parameter2"];
Response.Write("Parameter1
:"
+ param1.ToString());
Response.Write("Parameter1
:"
+ param2.ToString());
}
}
Source.aspx
Destination.aspx
Server side
Session : ASP.NET session state have several different storage options for session data.Option are
Session : ASP.NET session state have several different storage options for session data.Option are
determined by SessionStateMode enumeration.
1) InProc mode stores
session state in memory on the server. This is the default session state .Store session state and variable in local Web server.
2) StateServer mode,
which stores session state in a separate process.
Example
xml
version="1.0"?>
<configuration>
<system.web>
<pages
enableViewState="false"/>
<sessionState
mode="StateServer"
stateConnectionString="Your
Server IP"
cookieless="false"
timeout="20"/>
</system.web>
</configuration>
Session is globally
unique 120 bit identifier . Sessionid is pass from client to server
and server to client. State is managed by HttpSessionState class
which comes under System.Web.SessionState
Type | Name | What is ? |
---|---|---|
Property | Count | How many session state is there in the HttpSessionState collection. |
Property | IsNewSession | An indicator which tells that the Session has created in current request or not. |
Property | Item[Int32] | Get or Set Session value with index. |
Property | Item[String] | Get or Set Session value with name. |
Property | Keys | Get collection of keys for all value stored in HttpSessionState. |
Property | SessionID | Unique identifier for session. |
Property | Timeout | Get or Set time limit to terminate a session. |
Method | Add(String, Object) | Add new item to the HttpSessionState. |
Method | Clear | Clear all form HttpSessionState collection. |
Method | Remove | Delete particular item from HttpSessionState collection. |
Method | RemoveAll | Remove all from HttpSessionState collection. |
3) SQLServer mode
stores session state in a SQL Server database.
Example
xml
version="1.0"?>
<configuration>
<system.web>
<pages
enableViewState="false"/>
<sessionState
mode="SQLServer"
sqlConnectionString=""
/>
</system.web>
</configuration>
4) Custom mode, store
custom in storage.
5) Off mode disables
session state
Example : How data stored in session variable and retrieve after postback.
Aspx Page
<%@
Page
Language="C#"
AutoEventWireup="true"
CodeFile="Source.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:Button
ID="Button1"
runat="server"
Text="Button"
OnClick="Button1_Click"
/>
</div>
<asp:HiddenField
ID="HiddenField1"
runat="server"
/>
</form>
</body>
</html>
C# Code
using
System;
using
System.Web.UI.WebControls;
using
System.Web;
public
partial
class
Default
: System.Web.UI.Page
{
protected
void
Page_Load(object
sender, EventArgs
e)
{
if
(Session["StateValue"]
!= null)
{
string
myValue = Session["StateValue"].ToString();
Response.Write(myValue);
}
}
protected
void
Button1_Click(object
sender, EventArgs
e)
{
Session["StateValue"]
= "Asp.Net
State Management Session Example";
}
}
Output :
Application : Application State is a server side State Management technique. The information stored in application state are usable for all user in a particular application .Application state can store only small data . This is called application level state management . Global.asax is a file which is created automatically when an Asp.Net application created . This file contain application event like Application_Start ,Application_End when application variable is managed . Application variable clear automatically only when hosting server restart.
Example : The example shows that application variable Name,Roll,Address declared in Global.asax.The value is retrieved in the Default.aspx page .Output has shown below.
Application State
comes under HttpApplicationState . Application State is generally
used for site hit count. Formulti client application client name is
also assign to the Application State variable and for Multi Language
Global Application , language and currency code sometimes
considered in Application State variable. Here are some commonly used
attribute for Application State variables.
Type | Name | What is ? |
---|---|---|
Property | Count | How many item in HttpApplicationState collections. |
Property | Item[Int32] | Get or Set object on HttpApplicationState by Index. |
Property | Item[String] | Get or Set object on HttpApplicationState by name. |
Property | Keys | Get collection of keys from HttpApplicationState. |
- Below are the event where related to Application Event.
- Application_Start
- Application_End
- Application_Error
- Session_Start
- Session_End
Global.asax
<%@
Application
Language="C#"
%>
<script
runat="server">
void
Application_Start(object
sender, EventArgs
e)
{
Application["Name"]
= "John";
Application["Roll"]
= "12";
Application["Address"]
= "3A,CALIFORNIA";
}
void
Application_End(object
sender, EventArgs
e)
{
//
Code that runs on application shutdown
Application.Clear();
}
</script>
Default.aspx
<%@
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>
</div>
</form>
</body>
</html>
Default.aspx.cs
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)
{
Response.Write(Application["Name"].ToString());
Response.Write("");
Response.Write(Application["Roll"].ToString());
Response.Write("");
Response.Write(Application["Address"].ToString());
}
}
Output :
John
12
3A,CALIFORNIA
12
3A,CALIFORNIA
No comments:
Post a Comment