.NET内置对象之Session对象
2007-11-04 10:14
411 查看
Session对象
Session对象是HttpSessionState类的一个实例,其功能和Application对象类似,都是用来存储跨网页程序的变量或者对象,但Session对象和Application对象变量有些特性存在着差异。Session对象变量只针对单一网页使用者,也就是说各个连接的机器有各自的Session对象变量,不同的客户端无法互相存取。Application对象变量中止于停止IIS服务,但是Session对象变量中止于联机机器离线时,也就是当网页使用者关掉浏览器或超过设定Session变量对象的有效时间时,Session对象变量就会消失。
新建一个网站,包括两个网页,代码如下:
1、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>
<table style="width: 346px; " align="center">
<tr>
<td>
用户名:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
密码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Button ID="Button1" runat="server" Text="登录" OnClick="Button1_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection sqlconn = new SqlConnection("Data Source=(local);Database=Northwind;Uid=sa;Pwd=860712");
sqlconn.Open();
SqlCommand sqlcomm = sqlconn.CreateCommand();
sqlcomm.CommandText = "select count(*) from Region where RegionID='" + TextBox1.Text + "' and RegionDescription='" + TextBox2.Text + "'";
int count = Convert.ToInt32(sqlcomm.ExecuteScalar());//获取SQL语句的值强制转换成数值类型
if (count > 0)
{
Session["id"] = TextBox1.Text;
Session["name"] = TextBox2.Text;
Page.Response.Redirect("Default2.aspx");
}
else
{
Response.Write("<script language=javascript>alert('用户名或密码有误!');location='javascript:history.go(-1)'</srcript>");
return;
}
}
}
2、Default2.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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:Label ID="Label1" runat="server" Text="Label"></asp:Label>,您是管理员。
</div>
</form>
</body>
</html>
Default2.aspx.cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["name"].ToString();
}
}
Session对象是HttpSessionState类的一个实例,其功能和Application对象类似,都是用来存储跨网页程序的变量或者对象,但Session对象和Application对象变量有些特性存在着差异。Session对象变量只针对单一网页使用者,也就是说各个连接的机器有各自的Session对象变量,不同的客户端无法互相存取。Application对象变量中止于停止IIS服务,但是Session对象变量中止于联机机器离线时,也就是当网页使用者关掉浏览器或超过设定Session变量对象的有效时间时,Session对象变量就会消失。
新建一个网站,包括两个网页,代码如下:
1、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>
<table style="width: 346px; " align="center">
<tr>
<td>
用户名:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
密码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Button ID="Button1" runat="server" Text="登录" OnClick="Button1_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection sqlconn = new SqlConnection("Data Source=(local);Database=Northwind;Uid=sa;Pwd=860712");
sqlconn.Open();
SqlCommand sqlcomm = sqlconn.CreateCommand();
sqlcomm.CommandText = "select count(*) from Region where RegionID='" + TextBox1.Text + "' and RegionDescription='" + TextBox2.Text + "'";
int count = Convert.ToInt32(sqlcomm.ExecuteScalar());//获取SQL语句的值强制转换成数值类型
if (count > 0)
{
Session["id"] = TextBox1.Text;
Session["name"] = TextBox2.Text;
Page.Response.Redirect("Default2.aspx");
}
else
{
Response.Write("<script language=javascript>alert('用户名或密码有误!');location='javascript:history.go(-1)'</srcript>");
return;
}
}
}
2、Default2.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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:Label ID="Label1" runat="server" Text="Label"></asp:Label>,您是管理员。
</div>
</form>
</body>
</html>
Default2.aspx.cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["name"].ToString();
}
}
相关文章推荐
- JSP内置对象——session对象
- JSP九大内置对象详解全析(三):session对象
- 【.net】ASP.NET的六大内置对象
- Asp.net内置对象—Session对象
- .NET内置对象之Request对象
- JSP内置对象--session对象(getId(),getCreationTime(),getLastAccessedTime(),isNew(),invalidate(),setAttribute(),getAttribute())
- .NET内置对象之Application对象
- .net的所有内置对象
- ASP内置对象之Application对象&Server对象&Session对象
- .net 内置对象之Session对象和Session的过期时间
- ASP.net内置对象-Session对象
- ASP.NET内置对象Session对象
- .NET内置对象之Server对象
- JSP脚本中的9个内置对象---09 session对象
- .Net的内置对象之一 Request
- .net面试基本问题一 ASP.Net七大内置对象 (网络整理)
- .NET内置对象之Cookie对象
- JSP内置对象二(response和session对象)
- .NET内置对象之Cache对象
- JAVA自学系列教程:第四课,JSP内置对象及网站案例(第四节 session对象)