aspx代码:
<%@ 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> .dl { margin:0px auto; padding:0px; left:400px; width:304px; height:257px; } </style> </head> <body> <form id="form1" runat="server"> <div class="dl"> <h1> 登录</h1> <p> </p> <p> <asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </p> <p> <asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </p> <p> </p> <p> <asp:Button ID="Button1" runat="server" Text="登陆" OnClick="Button1_Click" /> <asp:Literal ID="Literal1" runat="server"></asp:Literal> </p> </div> </form> </body> </html>
cs代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class DengLu : System.Web.UI.Page { private UsersDataContext context = new UsersDataContext(); protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //取值 string uid = TextBox1.Text; string pwd = TextBox2.Text; //查数据库 var query = context.users.Where(p => p.UserName == uid && p.PassWord == pwd); if(query.Count()>0) { int state = query.First().State.Value; if (state == 0) { Literal1.Text = "<script type='Text/javascript'>alert('此账号未审核!');</script>"; } else if(state == 1) { Session["uid"] = uid; Response.Redirect("Main.aspx"); } else { Literal1.Text = "<script type='Text/javascript'>alert('此账号审核失败!');</script>"; } } else { Literal1.Text = "<script type='Text/javascript'>alert('用户名或密码错误!');</script>"; } } }