ASP.NET登录和注册(详细代码)

数据模型

public class Model
{
    public Model()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    private string _Ucode;
    /// 
    /// 账号
    /// 
    public string Ucode
    {
        get { return _Ucode; }
        set { _Ucode = value; }
    }
    private string _Umima;
    /// 
    /// 密码
    /// 
    public string Umima
    {
        get { return _Umima; }
        set { _Umima = value; }
    }
    private string _Uname;
    /// 
    /// 姓名
    /// 
    public string Uname
    {
        get { return _Uname; }
        set { _Uname = value; }
    }
    private bool _Usex;
    /// 
    /// 性别
    /// 
    public bool Usex
    {
        get { return _Usex; }
        set { _Usex = value; }
    }
    /// 
    /// 输出性别
    /// 
    public string Sex
    {
        get { return _Usex ? "男" : "女"; }
    }
    private DateTime _Ubirth;
    /// 
    /// 生日
    /// 
    public DateTime Ubirth
    {
        get { return _Ubirth; }
        set { _Ubirth = value; }
    }
    private string _Uminzu;
    /// 
    /// 民族编号
    /// 
    public string Uminzu
    {
        get { return _Uminzu; }
        set { _Uminzu = value; }
    }
    private string _Umzz;
    /// 
    /// 民族编号
    /// 
    public string Umzz
    {
        get { return _Umzz; }
        set { _Umzz = value; }
    }
    private string _Ummz;
    /// 
    /// 民族名字
    /// 
    public string Ummz
    {
        get { return _Ummz; }
        set { _Ummz = value; }
    }
    /// 
    /// 输出民族
    /// 
    public string MINZU
    {
        get { return new Operation().selectminzu1(_Uminzu); }
    }
}

数据访问类

public class Operation
{
    SqlConnection conn = null;
    SqlCommand com = null;
    public Operation()
    {
        conn = new SqlConnection("server=.;database=ChangYong;user=sa;pwd=123;");
        com = conn.CreateCommand();
    }
    /// 
    /// 查询全部数据
    /// 
    /// 
    public List<Model> SelectUtb()
    {
        List<Model> list = new List<Model>();
        com.CommandText = "select * from Utb";
        conn.Open();
        SqlDataReader dr = com.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                Model M = new Model();
                M.Ucode = dr["Ucode"].ToString();
                M.Umima = dr["Umima"].ToString();
                M.Uname = dr["Uname"].ToString();
                M.Usex = Convert.ToBoolean(dr["Usex"]);
                M.Ubirth = Convert.ToDateTime(dr["Ubirth"]);
                M.Uminzu = dr["Uminzu"].ToString();
                list.Add(M);
            }
        }
        conn.Close();
        return list;
    }

    /// 
    /// 登录
    /// 
    /// 
    /// 
    /// 
    public bool Login(string code, string mima)
    {
        com.CommandText = "select * from Utb where Ucode = @code and Umima = @mima";
        com.Parameters.Clear();
        com.Parameters.Add("@code", code);
        com.Parameters.Add("@mima", mima);
        conn.Open();
        SqlDataReader dr = com.ExecuteReader();
        bool has = dr.HasRows;
        conn.Close();
        return has;
    }


    /// 
    /// 查询全部民族
    /// 
    /// 
    public List<Model> selectminzu()
    {
        List<Model> list = new List<Model>();
        com.CommandText = "select * from Umz";
        conn.Open();
        SqlDataReader dr = com.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                Model M = new Model();
                M.Umzz = dr["Umzz"].ToString();
                M.Ummz = dr["Ummz"].ToString();
                list.Add(M);
            }
        }
        conn.Close();
        return list;
    }

    /// 
    /// 注册添加信息
    /// 
    public void insertUtb(Model M)
    {
        com.CommandText = "insert into Utb values(@Ucode,@Umima,@Uname,@Usex,@UbirThday,@Uminzu)";
        com.Parameters.Clear();
        com.Parameters.Add("@Ucode", M.Ucode);
        com.Parameters.Add("@Umima", M.Umima);
        com.Parameters.Add("@Uname", M.Uname);
        com.Parameters.Add("@Usex", M.Usex);
        com.Parameters.Add("@Ubirthday", M.Ubirth);
        com.Parameters.Add("@Uminzu", M.Uminzu);
        conn.Open();
        com.ExecuteNonQuery();
        conn.Close();
    }


    /// 
    /// 查询民族
    /// 
    /// 
    public string selectminzu1(string Umzz)
    {
        string minzu = "";
        com.CommandText = "select * from Umz where Umzz = @Umzz";
        com.Parameters.Clear();
        com.Parameters.Add("@Umzz", Umzz);
        conn.Open();
        SqlDataReader dr = com.ExecuteReader();
        if (dr.HasRows)
        {
            dr.Read();
            minzu = dr["Ummz"].ToString();
        }
        conn.Close();
        return minzu;
    }

    /// 
    /// 查询是否存在账号
    /// 
    /// 
    /// 
    public bool selectcode(string code)
    {
        com.CommandText = "select * from Utb where Ucode = @code";
        com.Parameters.Clear();
        com.Parameters.Add("@code", code);
        conn.Open();
        SqlDataReader dr = com.ExecuteReader();
        bool has = dr.HasRows;
        conn.Close();
        return has;
    }
}

登录html界面

<style>
        * {
            margin: 0px;
            padding: 0px;
        }

        #background {
            height: 600px;
            width: 100%;
            position: relative;
            background-image: url(图片1.jpg);
            background-repeat: no-repeat;
            background-size: cover;
        }

        #loginwindow {
            width: 40%;
            height: 40%;
            left: 50%;
            top: 35%;
            position: relative;
            background-color: #DCDCDC;
            border: 10px outset white;
        }

        #login {
            font-family: 微软雅黑;
            font-size: 20px;
            left: 20%;
            top: 30%;
            position: relative;
        }

        #TextBox1 {
            top: 22px;
            left: 35%;
            position: relative;
        }

        #TextBox2 {
            top: 27px;
            left: 35%;
            position: relative;
        }

        #Button1 {
            width: 70px;
            height: 30px;
            left: 5.5%;
            top: 80px;
            position: relative;
            background-color: #00FF7F;
        }

        #Button2 {
            width: 70px;
            height: 30px;
            left: 6%;
            top: 80px;
            position: relative;
            background-color: #FF6A6A;
        }

        #Label1 {
            color: red;
            left: -15%;
            top: 130px;
            position: relative;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div id="background">
            <div id="loginwindow">
                <div id="login">
                    用户名:<br />&nbsp&nbsp 码:
                </div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
                <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="登录" />
                <asp:Button ID="Button2" runat="server" Text="注册" />
                <asp:Label ID="Label1" runat="server"></asp:Label>
            </div>
        </div>
    </form>
</body>

登陆后台

protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;//登录点击事件
        Button2.Click += Button2_Click;//注册点击事件
    }

    void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default2.aspx");
    }

    void Button1_Click(object sender, EventArgs e)
    {
        bool Has = new Operation().Login(TextBox1.Text, TextBox2.Text);
        if (Has)
        {
            Response.Redirect("Default3.aspx");
        }
        else
        {
            Label1.Text = "账号密码错误!";
        }

    }

注册后台

protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
        if (IsPostBack == false)
        {
            Dt(1900, DateTime.Now.Year, DropDownList1);//添加年
            Dt(1, 12, DropDownList2);//添加月
            Dt(1, 31, DropDownList3);//添加日

            List<Model> list = new Operation().selectminzu();
            foreach (Model M in list)
            {
                ListItem Li = new ListItem();
                Li.Text = M.Ummz;
                Li.Value = M.Umzz;
                DropDownList4.Items.Add(Li);//添加民族
            }
        }
    }

    /// 
    /// 注册点击
    /// 
    /// 
    /// 
    void Button1_Click(object sender, EventArgs e)
    {
        foreach (Control Con in enrollwindow.Controls)
        {
            if (Con is TextBox)
            {
                TextBox TB = Con as TextBox;
                if (TB.Text == "")
                {
                    Label1.Text = "输入不能为空!";
                    return;
                }
                else
                {
                    if (TextBox2.Text != TextBox3.Text)
                    {
                        Label1.Text = "两次密码输入不一致!";
                        return;
                    }
                    else
                    {
                        bool Has = new Operation().selectcode(TextBox1.Text);
                        if (Has)
                        {
                            Label1.Text = "账号已存在!";
                            return;
                        }
                    }
                }
            }
        }
        Model M = new Model();
        M.Ucode = TextBox1.Text;
        M.Umima = TextBox2.Text;
        M.Uname = TextBox5.Text;
        M.Usex = RadioButton1.Checked;
        M.Ubirth = Convert.ToDateTime(DropDownList1.Text + "-" + DropDownList2.Text + "-" + DropDownList3.Text);
        M.Uminzu = DropDownList4.SelectedValue;
        Operation Op = new Operation();
        Op.insertUtb(M);
    }

    /// 
    /// 添加时间日期的方法
    /// 
    /// 
    /// 
    /// 
    public void Dt(int start, int end, DropDownList Dr)
    {
        for (int i = start; i <= end; i++)
        {
            ListItem Li = new ListItem();
            Li.Text = i.ToString();
            Li.Value = i.ToString();
            Dr.Items.Add(Li);
        }
    }

登录成功界面

<body>
    <form id="form1" runat="server">
        <h1>登录成功!</h1>
        <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <table>
                    <tr>
                        <td>账号</td>
                        <td>密码</td>
                        <td>姓名</td>
                        <td>性别</td>
                        <td>生日</td>
                        <td>民族</td>
                    </tr>
                </table>
            </HeaderTemplate>
            <ItemTemplate>
                <table>
                    <tr>
                        <td><%#Eval("Ucode") %></td>
                        <td><%#Eval("Umima") %></td>
                        <td><%#Eval("Uname") %></td>
                        <td><%#Eval("Sex") %></td>
                        <td><%#Eval("Ubirth","{0:yyyy年MM月dd日}") %></td>
                        <td><%#Eval("MINZU") %></td>
                    </tr>
                </table>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <table>
                    <tr style="background-color: #808080">
                        <td><%#Eval("Ucode") %></td>
                        <td><%#Eval("Umima") %></td>
                        <td><%#Eval("Uname") %></td>
                        <td><%#Eval("Sex") %></td>
                        <td><%#Eval("Ubirth","{0:yyyy年MM月dd日}") %></td>
                        <td><%#Eval("MINZU") %></td>
                    </tr>
                </table>
            </AlternatingItemTemplate>
            <FooterTemplate></FooterTemplate>
        </asp:Repeater>
    </form>
</body>

登录成功后台

 protected void Page_Load(object sender, EventArgs e)
     {
         List<Model> list = new Operation().SelectUtb();
         Repeater1.DataSource = list;//指向要绑定的数据
         Repeater1.DataBind();//绑定数据
     }

你可能感兴趣的:(Asp.Net,asp.net,服务器,数据库)