作业:实现用户注册功能
要求:
1.注册内容包括:用户名,密码,姓名,地址,手机,出生年月,性别,邮箱
2.注册时验证用户名是否存在
3.出生年月日让用户选择,而不是手动输入
前台 在浏览器中查看 (自己写)
--将年月写死,用javascript可以实现年月
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyWeb.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">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr><td>
<asp:Label ID="txtUserName" runat="server" Text="用户名:"></asp:Label></td><td>
<asp:TextBox ID="txbUserName" runat="server"></asp:TextBox></td></tr>
<tr><td>
<asp:Label ID="txtPwd" runat="server" Text="密码:"></asp:Label></td><td>
<asp:TextBox ID="txbPwd" runat="server"></asp:TextBox></td></tr>
<tr><td>
<asp:Label ID="txtName" runat="server" Text="姓名:"></asp:Label></td><td>
<asp:TextBox ID="txbName" runat="server"></asp:TextBox></td></tr>
<tr><td>
<asp:Label ID="txtAddress" runat="server" Text="地址:"></asp:Label></td><td>
<asp:TextBox ID="txbAddress" runat="server"></asp:TextBox></td></tr>
<tr><td>
<asp:Label ID="txtPhone" runat="server" Text="手机号:"></asp:Label></td><td>
<asp:TextBox ID="txbPhone" runat="server"></asp:TextBox></td></tr>
<tr><td>
<asp:Label ID="txtBrithday" runat="server" Text="出生年月:"></asp:Label></td><td>
<asp:DropDownList ID="DropDownBrithday" runat="server">
<asp:ListItem>1993-01</asp:ListItem>
<asp:ListItem>1993-02</asp:ListItem>
<asp:ListItem>1993-03</asp:ListItem>
<asp:ListItem>1993-04</asp:ListItem>
<asp:ListItem>1993-05</asp:ListItem>
<asp:ListItem>1993-06</asp:ListItem>
<asp:ListItem>1993-07</asp:ListItem>
<asp:ListItem>1993-08</asp:ListItem>
<asp:ListItem>1993-09</asp:ListItem>
<asp:ListItem>1993-10</asp:ListItem>
<asp:ListItem>1993-11</asp:ListItem>
<asp:ListItem>1993-12</asp:ListItem>
</asp:DropDownList>
</td></tr>
<tr><td>
<asp:Label ID="txtGender" runat="server" Text="性别:"></asp:Label></td><td>
<asp:TextBox ID="txbGender" runat="server"></asp:TextBox></td></tr>
<tr><td>
<asp:Label ID="txtE_mail" runat="server" Text="邮箱:"></asp:Label></td><td>
<asp:TextBox ID="txbE_mail" runat="server"></asp:TextBox></td></tr>
<tr><td>
<asp:Button ID="btnLogon" runat="server" Text="注册" οnclick="btnLogon_Click" /></td><td>
<asp:Label ID="txtLable" runat="server" Text=""></asp:Label></td><td>
<asp:Label ID="txtLabel2" runat="server" Text=""></asp:Label></td></tr>
</table>
</form>
</body>
</html>
后台连接数据库
protected void btnLogon_Click(object sender, EventArgs e)
{
string sqlserver = "Data Source=SXVPTNJT9Q07YYP;Initial Catalog=MyWeb;User ID=sa;Password=abcdef";
SqlConnection conn = new SqlConnection(sqlserver);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select count(*) from T_myweb where UserName=@UserName";
cmd.Parameters.AddWithValue("@UserName", txbUserName.Text);
int result = Convert.ToInt32(cmd.ExecuteScalar());
if (result > 0)
{
txtLable.Text = "已经存在相同用户名!";
}
else
{
cmd.Parameters.AddWithValue("UserName", txbUserName.Text);
cmd.Parameters.AddWithValue("tpassWord", txbPwd.Text);
cmd.Parameters.AddWithValue("Name", txbName.Text);
cmd.Parameters.AddWithValue("tAddress", txbAddress.Text);
cmd.Parameters.AddWithValue("Phone", txbPhone.Text);
cmd.Parameters.AddWithValue("Brithday", DropDownBrithday.Text);
cmd.Parameters.AddWithValue("Gender", txbGender.Text);
cmd.Parameters.AddWithValue("E_mail", txbE_mail.Text);
// string email1 = txbE_mail.Text;
// string[] email = email1.Split('@');
// foreach (string item in email)
// {
// if (email.Length > 0)
// {
// txtLabel2.Text = item;
// }
// }
txtLable.Text = "注册成功!";
}
}