设置登入页面

 <asp:Label ID="Label1" runat="server" Text="用户名" AssociatedControlID="TextBox1"></asp:Label>
        <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");
            }          
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值