WebForm1.aspx :-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ApplicationState1.aspx.cs" Inherits="ViewState_Session_ApplicationState.ApplicationState1" %>
<!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="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
style="height: 26px" Text="Button" />
</div>
</form>
</body>
</html>
ViewState1. ViewState of a web form is available only within that web form.
2. ViewState is stored on the page in a hidden field called _ViewState. Because of this, the ViewState, will be lost, if you navigate away from the page, or if the browser is closed.
3. ViewState is used by all asp.net controls to retain their state across postback.
ViewState1.aspx.cs :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ViewState_Session_ApplicationState
{
public partial class ViewState1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (ViewState["Clicks"] == null)
ViewState["Clicks"] = 0;
TextBox1.Text = ViewState["Clicks"].ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int clickcount =(int) ViewState["Clicks"] + 1;
TextBox1.Text = clickcount.ToString();
ViewState["Clicks"] = clickcount;
}
}
}
SessionState
1. SessionState variables are available across all pages, but only for a given single session. Session variables are like single-user global data.
2. SessionState variables are stored on the webserver.
3. SessionState variables are cleared, when the user session times out. The default is 20 minutes. This is configurable in web.config file.
SessionState1.aspx.cs :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ViewState_Session_ApplicationState
{
public partial class SessionState1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Clicks"] == null)
Session["Clicks"] = 0;
TextBox1.Text = Session["Clicks"].ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int clickcount = (int)Session["Clicks"] + 1;
TextBox1.Text = clickcount.ToString();
Session["Clicks"] = clickcount;
}
}
}
Application State
1. Application State variables are available across all pages and across all sessions. Application State variables are like muli-user global data.
2. Application State variables are stored on the web server.
3. Application State variables are cleared, when the process hosting the application is restarted.
ApplicationState1.aspx.cs :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ViewState_Session_ApplicationState
{
public partial class ApplicationState1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Application["Clicks"] == null)
Application["Clicks"] = 0;
TextBox1.Text = Application["Clicks"].ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int clickcount = (int)Application["Clicks"] + 1;
TextBox1.Text = clickcount.ToString();
Application["Clicks"] = clickcount;
}
}
}
No comments:
Post a Comment