C#简单功能(九)

9设计一基于web窗体的登录界面,窗体中控件居中显示。要求用户输入用户名和密码,若登录失败则显示用户名或密码错误,若登录成功则显示第二个web窗体。要求第二个web窗体进行验证用户是否登录,若未登录则跳转到第一个登录页面。

  1. 第一步:登录界面设计:

控件居中 :<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>
            &nbsp;</p>
        <p>
            &nbsp;</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>
            &nbsp;</p>
        <p>
            &nbsp;</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){}
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值