注:要先将信息存入数据库
1、首先文件菜单下,新建网站 选择 c#模板,Asp.net空网站,并选择文件位置。
2、添加新项,创建用户注册页面,这里直接用Default.aspx
3、需要用到两个必填验证控件,放置到,用户名和密码下面,和一个比较验证 放置到再输入密码下面,实现注册项简单验证。
4、创建Sqlserver数据库,命名为User.mdf,创建一个表为 Users,添加列 username、password,设置数据类型
5、放置一个数据源控件SqlDataSource控件到页面,配置数据源,名称设置为zhuce,选择表 Users,下一步。
6、前台页面基本就好了,需要注意的是:
设置好各控件ID等属性,将密码框textmode
设置为Password
,验证控件属性自己参考。
7、双击按钮,进入按钮事件后台代码处,输入如下代码:
Default.aspx.cs
web.config
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- SqlConnection cn=new SqlConnection(ConfigurationManager.ConnectionStrings["zhuce"].ToString());//获取使用连接字符串创建连接数据库对象
- string Search = "select* from Users where username=@un";//查询用户名
- SqlCommand cmdS = new SqlCommand(Search, cn);//使用Connection对象连接数据库
- cmdS.Parameters.AddWithValue("@un", txtName.Text);//添加参数和其值
- cn.Open();//打开数据库
- SqlDataReader dr = cmdS.ExecuteReader();//只读查询数据生成对象dr
- if (dr.HasRows)//如果返回记录,提示失败,关闭连接对象dr
- {
- lblTip.Text = "注册失败!已存在用户名:" + txtName.Text;//失败注册提示
- lblTip.ForeColor = System.Drawing.Color.Red;
- dr.Close();
- }
- else//未返回,继续注册
- {
- dr.Close();
- string sqlInsert = "insert into Users(username,password)values(@un,@pwd)";//定义类把要插入的值用参数传递
- SqlCommand cmdIn = new SqlCommand(sqlInsert, cn);//使用Connection对象连接数据库
- cmdIn.Parameters.AddWithValue("@un", txtName.Text);//添加参数和其值
- cmdIn.Parameters.AddWithValue("@pwd", txtPwd1.Text);//添加参数和其值
- cmdIn.ExecuteNonQuery();//执行添加记录
- lblTip.Text = "注册成功!用户名:" + txtName.Text + ";密码:" + txtPwd1.Text;//成功注册提示
- lblTip.ForeColor = System.Drawing.Color.Red;
- }
- cn.Close();//关闭数据库}
-
8、在 .cs后台文件添加命名空间:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
-
附完整代码:
Default.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!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">
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- SqlConnection cn=new SqlConnection(ConfigurationManager.ConnectionStrings["zhuce"].ToString());//获取使用连接字符串创建连接数据库对象
- string Search = "select* from Users where username=@un";//查询用户名
- SqlCommand cmdS = new SqlCommand(Search, cn);//使用Connection对象连接数据库
- cmdS.Parameters.AddWithValue("@un", txtName.Text);//添加参数和其值
- cn.Open();//打开数据库
- SqlDataReader dr = cmdS.ExecuteReader();//只读查询数据生成对象dr
- if (dr.HasRows)//如果返回记录,提示失败,关闭连接对象dr
- {
- lblTip.Text = "注册失败!已存在用户名:" + txtName.Text;//失败注册提示
- lblTip.ForeColor = System.Drawing.Color.Red;
- dr.Close();
- }
- else//未返回,继续注册
- {
- dr.Close();
- string sqlInsert = "insert into Users(username,password)values(@un,@pwd)";//定义类把要插入的值用参数传递
- SqlCommand cmdIn = new SqlCommand(sqlInsert, cn);//使用Connection对象连接数据库
- cmdIn.Parameters.AddWithValue("@un", txtName.Text);//添加参数和其值
- cmdIn.Parameters.AddWithValue("@pwd", txtPwd1.Text);//添加参数和其值
- cmdIn.ExecuteNonQuery();//执行添加记录
- lblTip.Text = "注册成功!用户名:" + txtName.Text + ";密码:" + txtPwd1.Text;//成功注册提示
- lblTip.ForeColor = System.Drawing.Color.Red;
- }
- cn.Close();//关闭数据库
- }
- }
- <?xml version="1.0"?>
- <!--
- 有关如何配置 ASP.NET 应用程序的详细信息,请访问
- http://go.microsoft.com/fwlink/?LinkId=169433
- -->
- <configuration>
- <connectionStrings>
- <add name="zhuce" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\User.mdf;Integrated Security=True;User Instance=True"
- providerName="System.Data.SqlClient" />
- </connectionStrings>
- <system.web>
- <compilation debug="true" targetFramework="4.0"/>
- </system.web>
- </configuration>
- <title></title>
- <style type="text/css">
- .style1
- {
- width: 350px;
- height: 200px;
- border: 1px solid #000000;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h3 style="text-align:center">用户注册</h3>
- <table align="center" cellpadding="0" cellspacing="0" class="style1">
- <tr>
- <td style="font-size: small; font-weight: bold; vertical-align: middle; text-align: right; width: 100px">
- 用户名:</td>
- <td style="font-size: small; font-weight: bold; vertical-align: middle; text-align: left; width: 150px">
- <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
- <br />
- <asp:RequiredFieldValidator ID="ValrName" runat="server"
- ControlToValidate="txtName" Display="Dynamic" ErrorMessage="用户名必填!"
- Font-Size="Small" ForeColor="#FF3300" SetFocusOnError="True"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td style="font-size: small; font-weight: bold; vertical-align: middle; text-align: right; width: 100px">
- 密码:</td>
- <td style="font-size: small; font-weight: bold; vertical-align: middle; text-align: left; width: 150px">
- <asp:TextBox ID="txtPwd1" runat="server" TextMode="Password"></asp:TextBox>
- <br />
- <asp:RequiredFieldValidator ID="ValrPwd1" runat="server"
- ControlToValidate="txtPwd1" Display="Dynamic" ErrorMessage="密码必填!"
- Font-Size="Small" ForeColor="#FF3300" SetFocusOnError="True"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td style="font-size: small; font-weight: bold; vertical-align: middle; text-align: right; width: 100px">
- 再输入密码:</td>
- <td style="font-size: small; font-weight: bold; vertical-align: middle; text-align: left; width: 150px">
- <asp:TextBox ID="txtPwd2" runat="server" TextMode="Password"></asp:TextBox>
- <br />
- <asp:CompareValidator ID="ValcPwd2" runat="server" ControlToCompare="txtPwd1"
- ControlToValidate="txtPwd2" Display="Dynamic" ErrorMessage="两次密码输入必须一致!"
- Font-Size="Small" ForeColor="#FF3300" SetFocusOnError="True"></asp:CompareValidator>
- </td>
- </tr>
- <tr>
- <td colspan="2"
- style="font-size: small; font-weight: bold; vertical-align: middle; text-align: center;">
- <asp:Button ID="btnSubmit" runat="server" Text="注册" οnclick="btnSubmit_Click" />
- </td>
- </tr>
- <tr>
- <td colspan="2"
- style="font-size: small; font-weight: bold; vertical-align: middle; text-align: center;">
- <asp:Label ID="lblTip" runat="server"></asp:Label>
- <br />
- <asp:SqlDataSource ID="SqlDataSource1" runat="server"
- ConnectionString="<%$ ConnectionStrings:zhuce %>"
- SelectCommand="SELECT * FROM [Users]"></asp:SqlDataSource>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>