<asp:TextBox ID="TextBox1" runat="server" TextMode="SingleLine"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="密码" AssociatedControlID="TextBox2"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
<asp:CheckBox ID="CheckBox1" runat="server" Text="记住密码"/>
<asp:Button ID="Button1" runat="server" Text="登入" OnClick="Button1_Click" />
。。。。。
public partial class login : System.Web.UI.Page
{BaseClass ba=new BaseClass();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{//不是第一次加载,创建cookies并检查是否有记录存在
HttpCookie cookies = Request.Cookies["USER"];
if (cookies != null)
{
this.CheckBox1.Checked = true;
this.TextBox1.Text = cookies["username"];
this.TextBox2.Attributes.Add("value", cookies["userpassword"]);
//密码框不能直接赋值,只能采用添加的方式
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("USER");
string username = TextBox1.Text;
string password = TextBox2.Text;
string strsql="select * from 用户表 where 管理员标志=1 and 用户名='"+TextBox1.Text.ToString()+ "'";
//用于执行的语句,当数据库中的用户名等于文本框中的值时
DataTable dt = new DataTable();
dt = ba.readtable(strsql);
//创建DataTable对象并调用readtable()方法
if (dt.Rows.Count > 0)
{//查询有数据库中有相应值时
if (dt.Rows[0]["密码"].ToString() == password)
{ //如果输入的密码在第一行中密码列中有
if (this.CheckBox1.Checked)
{//如果记住密码被选中,就保存在cookie中,有效区间有7天
cookie.Values.Add("username", this.TextBox1.Text.Trim());
cookie.Values.Add("userpassword", this.TextBox2.Text.Trim());
cookie.Expires = System.DateTime.Now.AddDays(7.0);
HttpContext.Current.Response.Cookies.Add(cookie);
}
else
{//否则,如cookie中有数据,则移除数据
if (cookie["USER"] != null)
{
HttpContext.Current.Response.Cookies.Remove("USER");
}
}
Response.Redirect("~/admin.html");
}
}
}
}