标题: 关于asp.net的session问题 [打印本页] 作者: 丁艳姣 时间: 2012-10-25 13:39 标题: 关于asp.net的session问题 我的登录页面的代码是:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace exam
{
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOk_Click(object sender, EventArgs e)
{
string uid = this.txtuid.Text.ToString();
string pwd = this.txtpwd.Text.ToString();
if (test(uid, pwd))
{
Session["step"] = uid;
Response.Redirect("houtai.aspx");
}
else {
Response.Write("<script>alert('用户名或密码错误!')</script>");
}
}
//检测用户名的函数
public bool test(string uid, string pwd)
{
SqlCommand cmd = DB.createSqlcommand("select count(*) from guanli where uid='"+uid+"' and pwd='"+pwd+"'");
if (Convert.ToInt32(cmd.ExecuteScalar()) == 1)
{
return true;
}
else {
return false;
}
}
}
}
后台首页的代码是:using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace exam
{
public partial class houtai : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToString(Session["step"]) == "")
{
Response.Write("<script>alert('你无权访问该页面!');window.location.href='login.aspx';</script>");
}
if (!IsPostBack)
{
dataBinder();
}
}
public void dataBinder()
{
SqlCommand cmd = DB.createSqlcommand("select * from exam");
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, "exam");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;