1.前台页面设计(登录)
<form id="form1" runat="server"> <asp:Label ID="Label1" runat="server" Text="登录名:"></asp:Label> <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> <asp:Label ID="lbUserName" runat="server" Text=""></asp:Label> <br /><br /> <asp:Label ID="Label2" runat="server" Text="密 码:"></asp:Label> <asp:TextBox ID="txtUserPass" runat="server"></asp:TextBox><br /><br /> <asp:LinkButton ID="likbtnRegister" runat="server" PostBackUrl="~/DefaultRegister.aspx" CausesValidation="false">注册新用户</asp:LinkButton><br /><br /> <asp:Button ID="btnLog" runat="server" Text="登录" OnClick="btnLog_Click" /> <asp:Button ID="btnReset" runat="server" Text="重置" OnClick="btnReset_Click" /> </form>
2.后天功能代码(登录)
protected SqlConnection getconnect() { //读取web.config节点配置 string strcon = ConfigurationManager.ConnectionStrings["login"].ConnectionString; //实例化sqlConnection对象 SqlConnection con1 = new SqlConnection(strcon); return con1; } /// <summary> /// 用户登录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnLog_Click(object sender, EventArgs e) { if (txtUserName.Text.Trim() == "") { lbUserName.Text = "用户名不能为空!"; lbUserName.ForeColor = System.Drawing.Color.Red; } else { lbUserName.Text = ""; //数据库连接 SqlConnection con = getconnect(); con.Open(); //创建sql语句,查询用户输入的用户名和密码是否正确 string strselect = "select count(*) from tb_userInfo where userName=@username and userPass=@userpass"; SqlCommand cmd = new SqlCommand(strselect, con); // 使用Parameters的Add方法添加参数类型 cmd.Parameters.Add(new SqlParameter("username", SqlDbType.VarChar, 20)); //设置Parameters的参数值 cmd.Parameters["username"].Value = txtUserName.Text; cmd.Parameters.Add(new SqlParameter("userpass", SqlDbType.VarChar, 50)); cmd.Parameters["userpass"].Value = txtUserPass.Text; if (Convert.ToInt32(cmd.ExecuteScalar()) > 0) { Response.Write("<script>alert('登录成功')</script>"); txtUserName.Text = ""; txtUserPass.Text = ""; } else { Response.Write("<script>alert('登录不成功')</script>"); } } } protected void btnReset_Click(object sender, EventArgs e) { txtUserName.Text = ""; txtUserPass.Text = ""; }
1.前台页面设计(注册)
<form id="form1" runat="server"> <asp:Label ID="Label1" runat="server" Text="用户名"></asp:Label> <asp:TextBox ID="txtName" runat="server" OnTextChanged="txtName_TextChanged"></asp:TextBox> <asp:Label ID="lbIsName" runat="server" Text=""></asp:Label> <asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="txtName"></asp:RequiredFieldValidator> <br /><br /> <asp:Label ID="Label2" runat="server" Text="密 码:"></asp:Label> <asp:TextBox ID="txtPass" runat="server"></asp:TextBox><br /><br /> <asp:Label ID="Label3" runat="server" Text="昵 称:"></asp:Label> <asp:TextBox ID="txtNickname" runat="server"></asp:TextBox><br /><br /> <asp:RadioButtonList ID="redlistSex" runat="server" RepeatDirection="Horizontal"> <asp:ListItem>男</asp:ListItem> <asp:ListItem>女</asp:ListItem> </asp:RadioButtonList> <asp:Label ID="Label4" runat="server" Text="电 话:"></asp:Label> <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox><br /><br /> <asp:Label ID="Label5" runat="server" Text="邮 箱:"></asp:Label> <asp:TextBox ID="txtEmaile" runat="server"></asp:TextBox><br /><br /> <asp:Label ID="Label6" runat="server" Text="城 市:"></asp:Label> <asp:TextBox ID="txtCity" runat="server"></asp:TextBox><br /><br /> <asp:Button ID="btRegister" runat="server" Text="注册" OnClick="btRegister_Click" /> <asp:Button ID="btnReturn" runat="server" Text="返回" PostBackUrl="~/DefaultLogin.aspx" CausesValidation="false" /> </form>
2.后天功能实现(注册)
protected SqlConnection getconnect() { //读取web.config节点配置 string strcon = ConfigurationManager.ConnectionStrings["login"].ConnectionString; //实例化sqlConnection对象 SqlConnection con1 = new SqlConnection(strcon); return con1; } /// <summary> /// 自定义一个isName()方法判断用户输入的用户名是否存在 /// </summary> /// <returns></returns> protected bool isName() { bool blisName = false; string sqlselect = "select count(*) from tb_userInfo where userName='" + txtName.Text + "'"; SqlConnection con = getconnect(); con.Open(); SqlCommand cmd = new SqlCommand(sqlselect, con); // 判断ExecuteScalar方法返回的参数是否大于0,大于0则表示用户名已存在 if (Convert.ToInt32(cmd.ExecuteScalar()) > 0) { blisName = true; } else { blisName = false; } return blisName; } /// <summary> /// 先判断用户填写的用户名是否为空,在判断用户名是否存在 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void txtName_TextChanged(object sender, EventArgs e) { if (txtName.Text == "") { lbIsName.Text = "用户名不能为空"; lbIsName.ForeColor = System.Drawing.Color.Red; } else { if (isName()) { lbIsName.Text = "用户名已经存在"; lbIsName.ForeColor = System.Drawing.Color.Red; } else { lbIsName.Text = "可以注册"; lbIsName.ForeColor = System.Drawing.Color.Blue; } } } /// <summary> /// 注册 /// </summary> /// <param name="ExecuteNonQuery()">执行SQL语句并返回受影响的行数</param> /// <param name="e"></param> protected void btRegister_Click(object sender, EventArgs e) { if (isName()) { lbIsName.Text = "用户名已经存在"; lbIsName.ForeColor = System.Drawing.Color.Red; } else { string username = txtName.Text.Trim(); string userpass = txtPass.Text.Trim(); string nickname = txtNickname.Text.Trim(); string sex = ""; if (redlistSex.SelectedValue.Trim() == "男") { sex = "男"; } else { sex = "女"; } string phone = txtPhone.Text.Trim(); string emaile = txtEmaile.Text.Trim(); string city = txtCity.Text.Trim(); string strinsert = "insert into tb_userInfo(userName,userPass,nickName,sex,phone,emaile,city) values('" + username + "','" + userpass + "','" + nickname + "','" + sex + "','" + phone + "','" + emaile + "','" + city + "')"; SqlConnection con = getconnect(); con.Open(); SqlCommand cmd = new SqlCommand(strinsert, con); if (cmd.ExecuteNonQuery() > 0) { Response.Write("<script>alert('注册成功')</script>"); txtName.Text = txtPass.Text = txtNickname.Text = txtPhone.Text = txtEmaile.Text = txtCity.Text = ""; } else { Response.Write("<script>alert('注册失败')</script>"); } } }