First step:
1 用户数据准备
在sql server中准备所需要的数据:
![](https://img.haomeiwen.com/i6310069/6b73c61d80d351fb.png)
表【user_inf】中的数据:
![](https://img.haomeiwen.com/i6310069/1abd8307963a7ec3.png)
Second step:
2 新建一个asp.net工程
![](https://img.haomeiwen.com/i6310069/2e2ef52f3c9685a1.png)
![](https://img.haomeiwen.com/i6310069/d2aa315060da7f67.png)
![](https://img.haomeiwen.com/i6310069/2f48b932ed2382f3.png)
![](https://img.haomeiwen.com/i6310069/1cdbb621b5b84094.png)
然后新建一个项目就完成了,因为之前选择的Empty,现在项目中什么都没有。
Third step:
3 添加页面
![](https://img.haomeiwen.com/i6310069/5ad2e0f03bc18f6b.png)
![](https://img.haomeiwen.com/i6310069/62f668d6c1e6a82f.png)
![](https://img.haomeiwen.com/i6310069/8ff71978c0666ebf.png)
Fourth step:
4 页面元素添加及代码控制
可以用拖控件的方式向页面添加元素或直接写代码的方式
此页面全部代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="登陆实现.login" %>
<%--<!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>
</head>
<body>
<form id="form1" action="login_deal.aspx" runat="server">
<asp:Label ID="Label1" runat="server" Text="用户账号或昵称:"></asp:Label>
<input id="Text1" type="text" name="number"/>
<br/>
<asp:Label ID="Label2" runat="server" Text="密码"></asp:Label>
<input id="Text2" type="text" name="pwd"/>
<input type="submit" />
</form>
</body>
</html>
预览一下效果:
第二个页面【login_deal.aspx】
添加方式如同前面一样,命名为login_deal,后缀名可加可不加,不加的话会自动添加。
第二个页面的内容不做任何改动;
在第二个页面内右键:
![](https://img.haomeiwen.com/i6310069/764e55e1d61c0120.png)
后台login_deal.aspx.cs 内全部代码如下:(数据库登录名视情况更改,数据库登陆密码亦然)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace 登陆实现
{
public partial class login_deal : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string user_number = Request.Form["number"].ToString();
string user_pwd = Request.Form["pwd"].ToString();
string connstring = "server=.;database=test_login;uid=数据库登陆名;pwd=数据库登陆密码;";//server=. 代表本地服务器,就是自己的电脑
//database=test_login 数据库名
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = conn.CreateCommand(); //创建数据库命令
cmd.CommandText = "select * from user_inf WHERE no=" + user_number+"AND pwd="+"'"+user_pwd+"'"; //创建查询语句
try
{
conn.Open();//打开数据库
SqlDataReader sdr = cmd.ExecuteReader(); //从数据库中读取数据流存入reader中
if (sdr.Read()) //从sdr中读取下一行数据,如果没有数据,sdr.Read()返回flase
{
HttpContext.Current.Session["UserId"] = user_number;
HttpContext.Current.Session["Userpwd"] = user_pwd;
Response.Redirect("welcome.aspx");
}
else
{
//Message.Text = "输入错误,请重新输入!";
// Response.Redirect("login.aspx");
// Response.Write("<script>confirm('输入账号或密码错误!');</script>");
Response.Write("<script language=javascript>alert('输入账号或密码错误!');window.location = 'login.aspx';</script>");
}
}
catch (Exception ee)
{
Response.Write("The connection is fair");
}
finally
{
conn.Close();
}
}
}
}
保存,然后添加第三个页面【welcome.aspx】
其页面全部代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="welcome.aspx.cs" Inherits="登陆实现.welcome" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
Welcome to loadin!
</div>
</form>
</body>
</html>
即可运行!(启动页面得是login.aspx页面,数据库服务得打开,用户名、密码得正确)
Fivth step:
5 效果
![](https://img.haomeiwen.com/i6310069/031b2403d49c85d2.png)
![](https://img.haomeiwen.com/i6310069/96926385b12d6e9f.png)
![](https://img.haomeiwen.com/i6310069/656f108c48f5db47.png)
网友评论