Step1. Firstly Add A Web Form index.aspx and just copy this below code...and paste your index.aspx page...
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>
<!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>ASP.net How to use session</title>
<style type="text/css">
.txtInput
{
width:250px; height:28px; padding:3px;
}
div
{
margin:5px;
}
.validator
{
color:Red;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<h3>How to use Session</h3>
<div>
Userame:<br />
<asp:TextBox ID="txtUserName" runat="server" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFirstName" runat="server" Display="Dynamic"
CssClass="validator" ControlToValidate="txtUserName"
ErrorMessage="Username is required"></asp:RequiredFieldValidator>
</div>
<div>
<asp:Button ID="btnCreateSession" runat="server"
Text="Click to Create Session" onclick="btnCreateSession_Click" />
<asp:Button ID="btnRetrieveSession" runat="server" CausesValidation="false"
Text="Click to Retrieve Session Value" onclick="btnRetrieveSession_Click" />
<asp:Button ID="btnRemoveSession" runat="server" CausesValidation="false"
Text="Click to Remove Session Value" onclick="btnRemoveSession_Click" />
<asp:Button ID="btnRemoveAll" runat="server" CausesValidation="false"
Text="Click to Remove All Sessions" onclick="btnRemoveAll_Click" />
</div>
<p>
Note: 1st create a session by providing user name in text field, then you can retrieve the value from session.
</p>
<div>
Value stored in Session:
<strong><asp:Label ID="lblSessionValue" runat="server"></asp:Label></strong>
</div>
</fieldset>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>
<!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>ASP.net How to use session</title>
<style type="text/css">
.txtInput
{
width:250px; height:28px; padding:3px;
}
div
{
margin:5px;
}
.validator
{
color:Red;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<h3>How to use Session</h3>
<div>
Userame:<br />
<asp:TextBox ID="txtUserName" runat="server" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFirstName" runat="server" Display="Dynamic"
CssClass="validator" ControlToValidate="txtUserName"
ErrorMessage="Username is required"></asp:RequiredFieldValidator>
</div>
<div>
<asp:Button ID="btnCreateSession" runat="server"
Text="Click to Create Session" onclick="btnCreateSession_Click" />
<asp:Button ID="btnRetrieveSession" runat="server" CausesValidation="false"
Text="Click to Retrieve Session Value" onclick="btnRetrieveSession_Click" />
<asp:Button ID="btnRemoveSession" runat="server" CausesValidation="false"
Text="Click to Remove Session Value" onclick="btnRemoveSession_Click" />
<asp:Button ID="btnRemoveAll" runat="server" CausesValidation="false"
Text="Click to Remove All Sessions" onclick="btnRemoveAll_Click" />
</div>
<p>
Note: 1st create a session by providing user name in text field, then you can retrieve the value from session.
</p>
<div>
Value stored in Session:
<strong><asp:Label ID="lblSessionValue" runat="server"></asp:Label></strong>
</div>
</fieldset>
</div>
</form>
</body>
</html>
2nd Step : Now double click on any button of index.aspx page---------------and write this follwing code on your index.aspx.cs page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCreateSession_Click(object sender, EventArgs e)
{
Session["Username"] = txtUserName.Text.Trim();
}
protected void btnRetrieveSession_Click(object sender, EventArgs e)
{
DisplaySessionValue();
}
protected void btnRemoveSession_Click(object sender, EventArgs e)
{
Session.Remove("Username");
DisplaySessionValue();
}
protected void btnRemoveAll_Click(object sender, EventArgs e)
{
Session.RemoveAll();
DisplaySessionValue();
}
private void DisplaySessionValue()
{
if (Session["Username"] != null)
lblSessionValue.Text = Convert.ToString(Session["Username"]);
else
lblSessionValue.Text = "No Value has been stored in session";
}
}
And Run This Programe
No comments:
Post a Comment