9设计一基于web窗体的登录界面,窗体中控件居中显示。要求用户输入用户名和密码,若登录失败则显示用户名或密码错误,若登录成功则显示第二个web窗体。要求第二个web窗体进行验证用户是否登录,若未登录则跳转到第一个登录页面。
- 第一步:登录界面设计:
控件居中 :<div style="text-align:center" >
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="登录界面.aspx.cs" Inherits="C_shape考核第九题.登录界面" %>
<!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 style="text-align:center" >
<p>
</p>
<p>
</p>
<p >
<asp:TextBox ID="TextBox1" runat="server" Width="59px" >用户名</asp:TextBox>
<asp:TextBox ID="txtID" runat="server" Width="290px"></asp:TextBox>
</p>
<p>
<asp:TextBox ID="TextBox2" runat="server" Width="59px">密 码</asp:TextBox>
<asp:TextBox ID="txtPwd" runat="server" Width="290px" TextMode="Password"></asp:TextBox>
</p>
<p>
</p>
<p>
</p>
<p>
<asp:Button ID="btn_login" runat="server" Height="30px" Text="登录" OnClick="btn_login_Click" Width="369px" />
</p>
</div>
</form>
</body>
</html>
2.登录界面代码设计:
双击登录按钮进入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.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace C_shape考核第九题
{
public partial class 登录界面 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["userName"] = null;
}
protected void btn_login_Click(object sender, EventArgs e)
{
//定义两个字符变量接收用户名和密码输入
string id = txtID.Text;
string pwd = txtPwd.Text;
//定义连接字符
string str = @"Data Source = LAPTOP-KKS0SHEJ;
Database = login;
Integrated Security = True;";
//创建数据库连接对象
SqlConnection conn = new SqlConnection(str);
//打开数据库连接
conn.Open();
//验证输入
if (txtID.Text == "" || txtPwd.Text == "")
{
//alert()弹窗函数
Response.Write("<script>alert('请输入非空的用户名或密码!');</script>");
}
else
{
string sql = "select * from login where id = '" + id + "' and pwd ='" + pwd + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
Session["userName"] = id;
Response.Redirect("验证界面.aspx");
}
else
{
Response.Write("<script>alert('用户名或密码错误');</script>");
}
}
}
}
}
提示: Response.Write("<script>alert('用户名或密码错误');</script>");
跳转: Response.Redirect("验证界面.aspx");
3.验证界面代码设计:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace C_shape考核第九题
{
public partial class 验证界面 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userName"]!=null)
{
Session.Clear();
}
else
{
Response.Write("<script>alert('请输入用户名或密码登录!');</script>");
Response.Redirect("登录界面.aspx");
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e){}
}
}