ASP.NET之注册实验

 今天来做个asp.net的注册实验,虽然简单,但能实验成功也算是对自己的鼓励。

步骤一: 准备数据库我使用的是sqlserver2000, 数据名称:aspnet ,账户表:account,结构如下图:

步骤二:在vs中新建页面文件reg.aspx,代码如下:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>用户注册</title>
</head>
<body>
    <form id="form1" runat="server">
    
    <div style="text-align:left">用户注册</div>
    
    <div>
        <asp:Label ID="Label1" runat="server" Text="用户名称:">
        </asp:Label><asp:TextBox ID="username" runat="server"></asp:TextBox>
            
        <asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
        <input id="password" type="password"  runat="server"/>
           
         <asp:Button ID="Submit" runat="server" Text="提交" onclick="Submit_Click" />
        
        </div>
            
    
    </form>
</body>
</html>

然后在“提交”按钮对应的click事件中添加代码,如下:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

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

    }

    /// <summary>
    ///  提交按钮被触发
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Submit_Click(object sender, EventArgs e)
    {
        string username = this.username.Text; //用户名
         string password = this.password.Value; //密码 注意:如果使用html控件就用value属性

        if(username == "")
        {
           Response.Write("<script>alert('用户名称不能为空!');</script>");
            //new MessageBox().showMessage("用户名称不能为空");
            return;
        }
        if (password == "")
        {
            Response.Write("<script>alert('密码不能为空!');</script>");
            return;
        }

        //获取Sqlconnection
        //SqlConnection conn = DAL.DBHelper.Connection;
        SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=sa;database=aspnet");

        //打开连接
        conn.Open();

        //sql
        string sql = "insert into account(username,password) values('"+username+"','"+password+"')";

        //sqlcommand
        SqlCommand cmd = new SqlCommand(sql, conn);
        
        //execute
        int counts = cmd.ExecuteNonQuery();

        if (counts > 0)
        {
            Response.Write("<script>alert('注册成功!');</script>");
           // return;
        }
        else
        {
            Response.Write("<script>alert('注册成功!');</script>");
           // return;
        }

        //关闭连接
        conn.Close();

    }
}


核心是使用cmd.ExecuteNonQuery();进行对数据库的insert操作,还有这里非空验证,按道理应该在前台js中,但我把它写在了后台程序中,有点不太 符合实际开发。

 

最后就是使测试阶段了,幸运的是还算顺利。


 有一点需要注意那就是服务器控件和html控件的取值问题:
string password = this.password.Value; //密码 注意:如果使用html控件就用value属性

string username = this.username.Text; //用户名

 

你可能感兴趣的:(html,String,server,asp.net,sqlserver,textbox)