当前位置:首页 > .NET内置对象之Session对象

.NET内置对象之Session对象

点击次数:1067  更新日期:2010-12-29
\n

Session对象


\n

Session对象是HttpSessionState类的一个实例,其功能和Application对象类似,都是用来存储跨网页程序的变量或者对象,但Session对象和Application对象变量有些特性存在着差异。Session对象变量只针对单一网页使用者,也就是说各个连接的机器有各自的Session对象变量,不同的客户端无法互相存取。Application对象变量中止于停止IIS服务,但是Session对象变量中止于联机机器离线时,也就是当网页使用者关掉浏览器或超过设定Session变量对象的有效时间时,Session对象变量就会消失。


\n

新建一个网站,包括两个网页,代码如下:


\n

1、Default.aspx代码:


\n

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>


\n

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>


\n

<html xmlns=”http://www.w3.org/1999/xhtml” >


\n

<head runat=”server”>


\n

<title>无标题页</title>


\n

</head>


\n

<body>


\n

<form id=”form1″ runat=”server”>


\n

<div>


\n

<table style=”width: 346px; ” align=”center”>


\n

<tr>


\n

<td>


\n

用户名:</td>


\n

<td>


\n

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox></td>


\n

</tr>


\n

<tr>


\n

<td>


\n

密码:</td>


\n

<td>


\n

<asp:TextBox ID=”TextBox2″ runat=”server” TextMode=”Password”></asp:TextBox></td>


\n

</tr>


\n

<tr>


\n

<td colspan=”2″ style=”text-align: center”>


\n

<asp:Button ID=”Button1″ runat=”server” Text=”登录” onClick=”Button1_Click” /></td>


\n

</tr>


\n

</table>


\n

</div>


\n

</form>


\n

</body>


\n

</html>


\n

Default.aspx.cs代码:


\n

using System;


\n

using System.Data;


\n

using System.Configuration;


\n

using System.Web;


\n

using System.Web.Security;


\n

using System.Web.UI;


\n

using System.Web.UI.WebControls;


\n

using System.Web.UI.WebControls.WebParts;


\n

using System.Web.UI.HtmlControls;


\n

using System.Data.SqlClient;


\n


\n

public partial class _Default : System.Web.UI.Page


\n

{


\n

protected void Page_Load(object sender, EventArgs e)


\n

{


\n


\n

}


\n

protected void Button1_Click(object sender, EventArgs e)


\n

{


\n

SqlConnection sqlconn = new SqlConnection(“Data Source=(local);Database=Northwind;Uid=sa;Pwd=860712″);


\n

sqlconn.Open();


\n

SqlCommand sqlcomm = sqlconn.CreateCommand();


\n

sqlcomm.CommandText = “select count(*) from Region where RegionID=’” + TextBox1.Text + “‘ and RegionDescription=’” + TextBox2.Text + “‘”;


\n

int count = Convert.ToInt32(sqlcomm.ExecuteScalar());//获取SQL语句的值强制转换成数值类型


\n

if (count > 0)


\n

{


\n

Session["id"] = TextBox1.Text;


\n

Session["name"] = TextBox2.Text;


\n

Page.Response.Redirect(“Default2.aspx”);


\n

}


\n

else


\n

{


\n

Response.Write(“<script language=javascript>alert(‘用户名或密码有误!’);location=’javascript:history.go(-1)’</srcript>”);


\n

return;


\n

}


\n

}


\n

}


\n

2、Default2.aspx代码:


\n

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default2.aspx.cs” Inherits=”Default2″ %>


\n

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>


\n

<html xmlns=”http://www.w3.org/1999/xhtml” >


\n

<head runat=”server”>


\n

<title>无标题页</title>


\n

</head>


\n

<body>


\n

<form id=”form1″ runat=”server”>


\n

<div>


\n

<asp:Label ID=”Label1″ runat=”server” Text=”Label”></asp:Label>,您是管理员。


\n

</div>


\n

</form>


\n

</body>


\n

</html>


\n

Default2.aspx.cs代码:


\n

using System;


\n

using System.Data;


\n

using System.Configuration;


\n

using System.Collections;


\n

using System.Web;


\n

using System.Web.Security;


\n

using System.Web.UI;


\n

using System.Web.UI.WebControls;


\n

using System.Web.UI.WebControls.WebParts;


\n

using System.Web.UI.HtmlControls;


\n


\n

public partial class Default2 : System.Web.UI.Page


\n

{


\n

protected void Page_Load(object sender, EventArgs e)


\n

{


\n

Label1.Text = Session["name"].ToString();


\n

}


\n

}


\n

\n