Introduction:
Hi.. Friends,
Today I explain that how to use session in asp.net so here i cover all events of session step by step now i going to elaborate this.
Step:1 The designing code goes here.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to use session in ASP.net</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 style="height: 281px; width: 1128px">
<h3 style="color: #249fa3">How to use Session in ASP.net</h3>
<div style="color: #249fa3; font-weight: bold">
Userame:<br />
<br />
<asp:TextBox ID="txtUserName" runat="server" MaxLength="50" Height="29px" Width="164px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFirstName" runat="server" Display="Dynamic"
CssClass="validator" ControlToValidate="txtUserName"
ErrorMessage="Username is required"></asp:RequiredFieldValidator>
<br />
</div>
<div>
<asp:Button ID="btnCreateSession" runat="server"
Text="Click to Create Session" OnClick="btnCreateSession_Click" BackColor="#249FA3" BorderColor="White" Font-Bold="True" ForeColor="White" Height="38px" />
<asp:Button ID="btnRetrieveSession" runat="server" CausesValidation="false"
Text="Click to Retrieve Session Value" OnClick="btnRetrieveSession_Click" BackColor="#249FA3" BorderColor="White" Font-Bold="True" ForeColor="White" Height="38px" />
<asp:Button ID="btnRemoveSession" runat="server" CausesValidation="false"
Text="Click to Remove Session Value" OnClick="btnRemoveSession_Click" BackColor="#249FA3" BorderColor="White" Font-Bold="True" ForeColor="White" Height="38px" />
<asp:Button ID="btnRemoveAll" runat="server" CausesValidation="false"
Text="Click to Remove All Sessions" OnClick="btnRemoveAll_Click" BackColor="#249FA3" BorderColor="White" Font-Bold="True" ForeColor="White" Height="38px" />
</div>
<p style="color: #249fa3">
Note: Firstly create a session by providing user name in text field, then you can retrieve the value from session.
</p>
<div style="color: #249fa3">
Value stored in Session:
<strong>
<asp:Label ID="lblSessionValue" runat="server"></asp:Label></strong>
</div>
</fieldset>
</div>
</form>
</body>
</html>
Step:2 Add some header files as i given blow.
Step:2 Add some header files as i given blow.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
Step:3 The CS code goes here
public partial class index : System.Web.UI.Page
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";
}
}
0 comments:
Post a Comment