State Management in Asp.Net Using C#
State Management in Asp.Net Using C#
HTTP is a State less Protocol that’s why State Management is used to maintain the state of between HTTP Request and Response.There are two ways one can maintain state.
1) Client Side State Management: It includes Cookies, Query String, Hidden Field, View State, Control State
2) Server Side State Management: It Includes Application Object, Session State .
Client Side State Management
1) Data Transfer Using Cookies :-
A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site. To store value from textbox.
A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site. To store value from textbox.
protected void btnSubmit_Click(object sender, EventArgs e)
{
HttpCookie CookieName = new HttpCookie("Value");
CookieName.Value = txtData.Text;
CookieName.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(CookieName);
Response.Redirect("RedirectPage.aspx");
}
{
HttpCookie CookieName = new HttpCookie("Value");
CookieName.Value = txtData.Text;
CookieName.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(CookieName);
Response.Redirect("RedirectPage.aspx");
}
And to retrieve data from cookie in RedirectPage.aspx page
In the Page_Load event write
String strData = Request.Cookies["CookieName "].Value);
Another way to use Cookies,
//Here we set a value of cookies
Response.Cookies["CookieName"].Value = "This is a Value of cookies";
//Here we set the age of cookies,in the form of Day ,Month,Years
Response.Cookies[“CookieName"].Expires = DateTime.Now.AddDays(1);
//Here We Get the value of cookies
Value of cookies= Request.Cookies["CookieName"].Value;
//Hear expires the cookies
Response.Cookies[“CookieName"].Expires = DateTime.Now.AddDays(-1);
2)Query String:-
Query strings are usually used to send information from one page to another page. They are passed along with URL in clear text.
Example,
Response.Redirect("Nextpage.aspx?Name="txtName.Text + "&LastName=" +txtLastName.Text);
Now get the value on Next.aspx
String Value1=Request.QueryString[“Name“];
String Value2=Request.QueryString[“Lastname“];
3)Hidden Field
ASP.NET allows you to store information in a Hidden Field control, which renders as a standard HTML hidden field. A hidden field does not render visibly in the browser, but you can set its properties just as you can with a standard control. When a page is submitted to the server, the content of a hidden field is sent in the HTTP form collection along with the values of other controls. A hidden field acts as a repository for any page-specific information that you want to store directly in the page.
Example,
Using System.Web.UI.HtmlControls.HtmlInputHidden Hidden1;
//to assign a value to Hidden field
Hidden1.Value="This is a Hidden Field Value";
//to retrieve a value
string str=Hidden1.Value;
System.Web.UI.HtmlControls.HtmlInputHidden Hidden1;
//to assign a value to Hidden field
Hidden1.Value="Create hidden fields";
//to retrieve a value
string str=Hidden1.Value;
4) View State
In this View State the object of view state can hold the value on the single page .
Example,
ViewState[“ViewState_Value”]=”This is a Value of View State”;
//Retrieve the value
String value= ViewState[“ViewState_Value”].ToString();
5 Control State
Control State is new mechanism in ASP.NET which addresses some of the shortcomings of View State. Control state can be used to store critical, private information across post backs. Control state is another type of state container reserved for controls to maintain their core behavioral functionality whereas View State only contains state to maintain control's contents (UI). Control State shares same memory data structures with View State. Control State can be propagated even though the View State for the control is disabled.
Server Side State Management
1) Session State
Session hold the value throught out the whole application . In this , the Session object is create on the Server and Manage at the client .
Example ,
//Create a Session
Session[“Session_Obj”] =”This is a Value of Session”;
//Retrive the value of session
String Value=Session[“Session_Obj”].ToString();
//How to Expire the Session
This Method Used to remove session when the specific session have to remove.
Session.Contents.Remove("Session_Obj");
This Method is Used When we need to remove all session objects in the application.
Session.Abandon();
2) Application State
ASP.NET allows you to save values using application state which is an instance of the HTTPApplication class — for each active Web application. Application state is a global storage mechanism that is accessible from all pages in the Web application. Thus, application state is useful for storing information that needs to be maintained between server round trips and between requests for pages.
Example.
//We Use application. lock event to avoid data conflicts.
Application.Lock();
Application["App_Data"]="Value of the Application State";
Application.UnLock();
I hope this will help you the understand how to Manage State in Asp.Net
Rajesh
very good
ReplyDelete