20151221:Web复习:登陆

前台HTML代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Denglu.aspx.cs" Inherits="Denglu" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">

                * {
                    margin: 0px auto;
                    padding: 0px;
                }
     </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="aa" style="width:400px">
        <h1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h1>
        <h1>&nbsp;</h1>
        <h1>&nbsp;</h1>
        <h1>&nbsp;</h1>
        <h1>&nbsp;</h1>
        <h1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 登录</h1>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>
        <asp:TextBox ID="txtUid" runat="server"></asp:TextBox>
        <br />
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
        <asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>
        <br />
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="登陆" OnClick="Button1_Click" />
    
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    
    </div>
    </form>
</body>
</html>

后台C#代码:

public partial class Denglu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextDataContext context = new TextDataContext();
        //取值
        string uid = txtUid.Text;
        string pwd = txtPwd.Text;
        //查询
        var query = context.login1.Where(p => p.username == uid && p.password == pwd);
        //判断
        if (query.Count() > 0)
        {
            Session["uid"] = uid;
            Response.Redirect("Main.aspx");
        }
        else
        {
            Literal1.Text = "<script type='Text/javascript'>alert('用户名或密码错误!');</script>";
        }
    }
}

posted @ 2015-12-24 22:33  m-n  阅读(188)  评论(0编辑  收藏  举报