您的位置:首页 > 其它

.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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: