📄 adduser.aspx
字号:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head><title>欣欣在线论坛新用户注册</title>
<style type="text/css">
<!--
td{font-size:9pt;}
-->
</style>
<script language="C#" runat="server">
String strConn;
SqlConnection cn;
void Page_Load(Object sender, EventArgs e)
{
// 从Web.config文件中读取连接字符串
strConn=ConfigurationSettings.AppSettings["eForum"];
}
void ValidationNickname(object source, ServerValidateEventArgs args)
{
// 连接到eForum数据库
cn=new SqlConnection(strConn);
// 打开连接
cn.Open();
// 构造SQL语句,该语句检查昵称是否被抢注
String strSQL="select * from RegUsers where NickName='"+nickname.Text+"'";
// 创建Command对象
SqlCommand cm=new SqlCommand(strSQL,cn);
// 执行ExecuteReader()方法
SqlDataReader dr=cm.ExecuteReader();
if (dr.Read()) {
args.IsValid=false; // 昵称被抢注
} else {
args.IsValid=true; // 昵称没被抢注
}
// 关闭连接
cn.Close();
}
void ValidationBirthday(object source, ServerValidateEventArgs args)
{
try {
DateTime d=DateTime.Parse(args.Value);
args.IsValid=true; // 是日期
}
catch {
args.IsValid=false; // 不是日期
}
}
protected void RegBtn_Click(object sender, EventArgs e)
{
if (Page.IsValid) {
// 连接到eForum数据库
cn=new SqlConnection(strConn);
// 打开连接
cn.Open();
// 构造SQL语句,该语句往RegUsers表中添加一条记录
String strSQL="insert into RegUsers (NickName,Password,Sex,Birthday,"+
"Education,Hobby,Brief,HomePage,LoginTime) values (@NickName,"+
"@Password,@Sex,@Birthday,@Education,@Hobby,@Brief,"+
"@HomePage,@LoginTime)";
// 创建Command对象
SqlCommand cm=new SqlCommand(strSQL,cn);
// 添加参数
cm.Parameters.Add(new SqlParameter("@NickName",SqlDbType.NVarChar,20));
cm.Parameters.Add(new SqlParameter("@Password",SqlDbType.NVarChar,20));
cm.Parameters.Add(new SqlParameter("@Sex",SqlDbType.VarChar,2));
cm.Parameters.Add(new SqlParameter("@Birthday",SqlDbType.DateTime));
cm.Parameters.Add(new SqlParameter("@Education",SqlDbType.NVarChar,10));
cm.Parameters.Add(new SqlParameter("@Hobby",SqlDbType.NVarChar,16));
cm.Parameters.Add(new SqlParameter("@Brief",SqlDbType.NVarChar,255));
cm.Parameters.Add(new SqlParameter("@HomePage",SqlDbType.NVarChar,30));
cm.Parameters.Add(new SqlParameter("@LoginTime",SqlDbType.DateTime));
// 给参数赋值
cm.Parameters["@NickName"].Value=nickname.Text;
cm.Parameters["@Password"].Value=pwd.Text;
if (Male.Checked) cm.Parameters["@Sex"].Value="男";
if (Female.Checked) cm.Parameters["@Sex"].Value="女";
cm.Parameters["@Birthday"].Value=birthday.Text;
cm.Parameters["@Education"].Value=edu.SelectedItem.Value;
cm.Parameters["@Hobby"].Value=hobby.SelectedItem.Value;
cm.Parameters["@Brief"].Value=brief.Text;
cm.Parameters["@HomePage"].Value=homepage.Text;
cm.Parameters["@LoginTime"].Value=DateTime.Today;
// 执行ExecuteNonQuery()方法
cm.ExecuteNonQuery();
// 关闭连接
cn.Close();
// 保存当前用户的昵称
Session["UserName"]=nickname.Text;
// 进入主画面
Response.Redirect("index.aspx");
}
}
</script>
</head>
<body background="./images/gray.jpg">
<form runat="server">
<table align="center" border="0" bgcolor="#FFE99D" width="90%" cellpadding="2">
<tr><td valign="middle" align="left" width="90%" colspan="2" bgcolor="#FFF9C4">
<div align="center">
<font color="#830701">欣欣在线论坛新用户注册</font>
</td></tr>
<tr align="center">
<td valign="middle" align="left" width="90%" colspan="2" bgcolor="#B3C4FF">
<font color="#FF0000">请输入您的昵称、密码及相关资料</font>
<font color="#0000FF">(**表示必须要填)</font>
</td></tr>
<tr align="center">
<td valign="middle" align="left" width="10%" bgcolor="#FEFFEC">
昵 称:</td>
<td bgcolor="#FEFFEC" align="left">
<asp:TextBox id="nickname" style="height:19px;width:100px"
Size="20" MaxLength="12" runat="server" />**
<asp:RequiredFieldValidator id="RFV1"
ControlToValidate="nickname"
Display="dynamic" runat="server">不能为空
</asp:RequiredFieldValidator>
<asp:CustomValidator id="cv1"
ControlToValidate="nickname"
OnServerValidate="ValidationNickname"
ForeColor="red" ErrorMessage="昵称被抢注!"
Display="static" runat="server"/>
</td></tr>
<tr align="center">
<td valign="middle" align="left" width="10%" bgcolor="#FEFFEC">
输入密码:</td>
<td bgcolor="#FEFFEC" align="left">
<asp:TextBox id="pwd" TextMode="password"
style="height:19px;width:100px"
Size="20" MaxLength="10" runat="server" />**
<asp:RequiredFieldValidator id="RFV2"
ControlToValidate="pwd"
Display="dynamic" runat="server">不能为空
</asp:RequiredFieldValidator>
</td></tr>
<tr align="center">
<td valign="middle" align="left" width="10%" bgcolor="#FEFFEC">
确认密码:</td>
<td bgcolor="#FEFFEC" align="left">
<asp:TextBox id="pwd2" TextMode="password"
style="height:19px;width:100px"
Size="20" MaxLength="10" runat="server" />**
<asp:CompareValidator id="CV"
ControlToValidate="pwd"
ControlToCompare="pwd2"
Type="String" Operator="Equal"
Display="dynamic" runat="server">密码不一致
</asp:CompareValidator>
</td></tr>
<tr align="center">
<td valign="middle" align="left" width="10%" bgcolor="#FEFFEC">
出生日期:</td>
<td bgcolor="#FEFFEC" align="left">
<asp:TextBox id="birthday" style="height:19px;width:300px"
Size="30" MaxLength="12" runat="server" />**
<asp:CustomValidator id="cv2"
ControlToValidate="birthday"
OnServerValidate="ValidationBirthday"
ForeColor="red"
ErrorMessage="不是日期!" Display="static" runat="server" />
</td></tr>
<tr align="center">
<td valign="middle" align="left" width="10%" bgcolor="#FFFDE6">
个人主页</td>
<td bgcolor="#FFFDE6" align="left">
<asp:TextBox id="homepage" style="height:19px;width:360px"
Size="50" Text="http://" runat="server" />
</td></tr>
<tr align="center">
<td valign="middle" align="left" width="10%" bgcolor="#FFFDE6">
性 别:</td>
<td bgcolor="#FFFDE6" align="left">
<asp:RadioButton id="Male" Text="男" GroupName="Sex"
Checked="true" runat="server" />
<asp:RadioButton id="Female" Text="女"
GroupName="Sex" runat="server" />
</td><tr>
<tr align="center">
<td valign="middle" align="left" width="10%" bgcolor="#FFFDE6">
教育程度:</td>
<td bgcolor="#FFFDE6" align="left">
<asp:DropDownList id="edu" size="1" runat="server">
<asp:ListItem selected="true" value="本科">本科</asp:ListItem>
<asp:ListItem value="硕士">硕士</asp:ListItem>
<asp:ListItem value="博士">博士</asp:ListItem>
<asp:ListItem value="本科以下">本科以下</asp:ListItem>
</asp:DropDownList>
</td><tr>
<tr align="center">
<td valign="middle" align="left" width="10%" bgcolor="#FFFDE6">
爱 好:</td>
<td bgcolor="#FFFDE6" align="left">
<asp:DropDownList id="hobby" size="1" runat="server">
<asp:ListItem selected="true" value="读书">读书</asp:ListItem>
<asp:ListItem value="打篮球">打篮球</asp:ListItem>
<asp:ListItem value="踢足球">踢足球</asp:ListItem>
<asp:ListItem value="玩游戏">玩游戏</asp:ListItem>
<asp:ListItem value="下棋">下棋</asp:ListItem>
<asp:ListItem value="看电影">看电影</asp:ListItem>
<asp:ListItem value="网上聊天">网上聊天</asp:ListItem>
<asp:ListItem value="看小说">看小说</asp:ListItem>
<asp:ListItem value="泡吧">泡吧</asp:ListItem>
<asp:ListItem value="听音乐">听音乐</asp:ListItem>
</asp:DropDownList>
</td></tr>
<tr align="center">
<td valign="middle" align="left" width="10%" bgcolor="#FFFEF0">
自我介绍:</td>
<td bgcolor="#FFFEF0" align="left">
<asp:TextBox id="brief" Rows="5" Columns="60"
TextMode="Multiline" runat="server" />
</td></tr>
<tr><td width="100%" align="center" colspan="2" bgcolor="#FFFFFF">
<asp:Button Text="注册" OnClick="RegBtn_Click" runat="server" />
</td></tr>
</table>
</form>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -