【Asp.Net】登陆注册+sql server增删改查 小demo

由于只注重主要功能实现,并未优化前端页面,所以看起来比较原始。

采用webform技术

三层架构 + webform显示

  1. Models层,数据模型层
  2. BLL层,业务逻辑层 reference Models and DAO
  3. DAO层,数据访问层 reference Models
  4. webform reference Bll and models

(1)登陆部分

login.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <center>
        <div>
            登录页面<br />
            <br />
            登录名:<asp:TextBox ID="login_textbox" runat="server"></asp:TextBox>
            <br />
            <br />
            密码:<asp:TextBox ID="password_textbox" runat="server" TextMode="Password"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="login_button" runat="server" OnClick="login_button_Click" Text="登陆" />
&nbsp;&nbsp;&nbsp;
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="注册" />
        </div>
        </center>
    </form>
</body>
</html>

 

login.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;

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

    }

    protected void login_button_Click(object sender, EventArgs e)
    {

        if (login_textbox.Text.Length != 0 && password_textbox.Text.Length != 0)
        {
            Models.User loginuser = new Models.User();
            loginuser.UserId = int.Parse(login_textbox.Text);
            loginuser.UserPassword = password_textbox.Text;
            try
            {
                DataSet loginset = new BLL.UserManager().getpassword(loginuser.UserId);
                if (loginuser.UserPassword == loginset.Tables[0].Rows[0].ItemArray[5].ToString())
                {
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('ok');</script>");
                    Session["UserId"] = loginuser.UserId;
                    Session.Timeout = 681;
                    Response.Redirect("main.aspx");
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('密码错误');</script>");
                }
            }
            catch
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('用户名无效,请先注册');</script>");
            }

        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('用户名和密码不能为空');</script>");
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("register.aspx");
    }
}

 

(2)注册部分

register.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <center>
        <div>
            注册页面<br />
            <br />
            UserId:<asp:TextBox ID="register_userid_textbox" runat="server"></asp:TextBox>
            <br />
            <br />
            UserName:<asp:TextBox ID="register_username_textbox" runat="server"></asp:TextBox>
            <br />
            <br />
            FirstName:<asp:TextBox ID="register_firstname_textbox" runat="server"></asp:TextBox>
            <br />
            <br />
            LastName:<asp:TextBox ID="register_lastname_textbox" runat="server"></asp:TextBox>
            <br />
            <br />
            Department:<asp:DropDownList ID="register_department_dropdownlist" runat="server">
                <asp:ListItem Value="1">IT</asp:ListItem>
                <asp:ListItem Value="2">HR</asp:ListItem>
                <asp:ListItem Value="3">SUPPORT</asp:ListItem>
            </asp:DropDownList>
            <br />
            <br />
            UserPassword:<asp:TextBox ID="register_password_textbox" runat="server"></asp:TextBox>
            <br />
            <br />
            UserPassword_again:<asp:TextBox ID="register_password_again_textbox" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="register_button" runat="server" OnClick="register_button_Click" Text="Register" />
        </div>
            </center>
    </form>
</body>
</html>

register.aspx.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 register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected bool isempty(Models.User user)
    {
        if (user.UserId != 0 && user.UserName.Length != 0 && user.UserFirstName.Length != 0 && user.UserLastName.Length != 0 && user.DepartmentId != 0 && user.UserPassword.Length != 0)
        {
            return true;
        }

        else
        {
            return false;
        }
        
    }
    protected void register_button_Click(object sender, EventArgs e)
    {
        try
        {
            Models.User registeruser = new Models.User();
            registeruser.UserId = int.Parse(register_userid_textbox.Text);
            registeruser.UserName = register_username_textbox.Text.ToString();
            registeruser.UserFirstName = register_firstname_textbox.Text.ToString();
            registeruser.UserLastName = register_lastname_textbox.Text.ToString();
            registeruser.DepartmentId = int.Parse(register_department_dropdownlist.SelectedValue);
            registeruser.UserPassword = register_password_textbox.Text.ToString();

            if (isempty(registeruser) == true)
            {

                if (register_password_textbox.Text.ToString().Equals(register_password_again_textbox.Text.ToString()))
                {
                    int result = new BLL.UserManager().add(registeruser);
                    if (result != 0)
                    {
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('注册成功');</script>");
                        Response.Redirect("login.aspx");
                    }
                    else
                    {
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('注册失败,请重新注册');</script>");
                    }
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('两次密码输入不一样');</script>");
                }

            }
            if (isempty(registeruser) == false)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('请全部填写');</script>");
            }
        }
        catch
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('请重新填写');</script>");
        }
        
    }
}

(3)增删改查部分

编辑状态

main.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="height: 607px; width: 1367px">
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
        <div style="height: 587px; width: 1512px">
            登录用户的用户ID:<asp:Label ID="userid_label" runat="server"></asp:Label>
&nbsp;<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="注销" />
            <br />
            <br />
            用户Id:<asp:TextBox ID="userid_textbox" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp; FirstName:<asp:TextBox ID="firstname_textbox" runat="server"></asp:TextBox>
&nbsp;&nbsp; 部门:<asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem Value="1">IT</asp:ListItem>
                <asp:ListItem Value="2">HR</asp:ListItem>
                <asp:ListItem Value="3">SUPPORT</asp:ListItem>
            </asp:DropDownList>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 部门: 
            <asp:DropDownList ID="DropDownList2" runat="server">
                <asp:ListItem Value="1">IT</asp:ListItem>
                <asp:ListItem Value="2">HR</asp:ListItem>
                <asp:ListItem Value="3">SUPPORT</asp:ListItem>
            </asp:DropDownList>
            <br />
            <br />
            用户姓名:<asp:TextBox ID="username_textbox" runat="server"></asp:TextBox>
&nbsp;&nbsp; LastName:<asp:TextBox ID="lastname_textbox" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;密码: 
            <asp:TextBox ID="password_textbox" runat="server" Width="67px"></asp:TextBox>
&nbsp;
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="增加用户" />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="按部门查询" />
&nbsp;&nbsp;
            <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="查询全部" />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <br />
            <br />
            <br />
            
            
            
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" GridLines="None" Height="470px"  OnRowDeleting="GridView1_RowDeleting" Width="100%" CellSpacing="1" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                <Columns>
                    <asp:BoundField DataField="UserID" HeaderText="用户id" />
                    <asp:BoundField DataField="UserName" HeaderText="用户姓名" />
                    <asp:BoundField DataField="UserFirstName" HeaderText="FirstName" />
                    <asp:BoundField DataField="UserLastName" HeaderText="LastName" />
                    <asp:BoundField DataField="DepartmentId" HeaderText="部门id" />
                    
                    <asp:BoundField DataField="UserPassword" HeaderText="password" />
                    
                    <asp:CommandField ShowDeleteButton="True" HeaderText="操作" ShowEditButton="True" />
                </Columns>
                <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
                <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
                <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
                <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#594B9C" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#33276A" />
            </asp:GridView>
            
            <br />
        </div>
        
    </form>
</body>
</html>

main.aspx.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 main : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //检查.aspx页面是否为传递回服务器的页面,常用于判断页面是否为首次加载
        if (!IsPostBack)
        {
            if (Session["UserId"]!=null)
            {
                userid_label.Text = Session["UserId"].ToString();
                bind();
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('请先登录');</script>");
                Response.Redirect("login.aspx");
            }
        }
    }

    private void bind()
    {
        GridView1.DataSource = new BLL.UserManager().getalllist();
        GridView1.DataKeyNames = new string[] { "UserId" };//主键
        GridView1.DataBind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('留言成功');</script>");
        int deleteid =int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        //Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('"+deleteid+"');</script>");
        bool result = new BLL.UserManager().delete(deleteid);
        if(result == true)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('删除成功');</script>");
        }
        if(result == false)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('删除失败,请重新操作');</script>");
        }
        bind();

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridView1.EditIndex = -1;
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string username = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
        string userfirstname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
        string userlastname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
        int departmentid = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text);
        string userpassword = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text;

        Models.User upuser = new Models.User();
        upuser.UserId = id;
        upuser.UserName = username;
        upuser.UserFirstName = userfirstname;
        upuser.UserLastName = userlastname;
        upuser.DepartmentId = departmentid;
        upuser.UserPassword = userpassword;
        //GridView1.EditIndex = -1;
        bool result = new BLL.UserManager().update(upuser);
        if(result==true)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('修改成功');</script>");
        }
        if (result == false)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('修改失败,请重新修改');</script>");
        }

        bind();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        
        if (userid_textbox.Text.Length!=0)
        {
            try
            {
                Models.User adduser = new Models.User();
                adduser.UserId = int.Parse(userid_textbox.Text);
                adduser.UserName = username_textbox.Text;
                adduser.UserFirstName = firstname_textbox.Text;
                adduser.UserLastName = lastname_textbox.Text;
                adduser.DepartmentId = int.Parse(DropDownList1.SelectedValue);
                adduser.UserPassword = password_textbox.Text;
                //Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('" + adduser.DepartmentId + "');</script>");
                int result = new BLL.UserManager().add(adduser);
                if (result != 0)
                {
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('修改成功');</script>");
                    userid_textbox.Text = null;
                    username_textbox.Text = null;
                    firstname_textbox.Text = null;
                    lastname_textbox.Text = null;
                    password_textbox.Text = null;
                }
                if (result == 0)
                {
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('修改失败,请重新修改');</script>");
                }
                bind();
            }
            catch
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('注意用户id只能为数字,重新填写');</script>");
            }
        }
        if(userid_textbox.Text.Length == 0)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "js", "<script>alert('用户id不能为空');</script>");
        }
        
        
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int departmentid = int.Parse(DropDownList2.SelectedValue);
        GridView1.DataSource = new BLL.UserManager().getdepartmentlist(departmentid);
        GridView1.DataKeyNames = new string[] { "UserId" };//主键
        GridView1.DataBind();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        bind();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("login.aspx");
    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        Session.Abandon();
    }
}

 

阅读更多

没有更多推荐了,返回首页