登录介绍
首先添加登录窗口ManageLogin.aspx,然后写登录代码,包含验证码这一要点
protected void btnLogin_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtName.Text)|| string.IsNullOrEmpty(txtPwd.Text) || string.IsNullOrEmpty(txtValid.Text)) { Page.RegisterStartupScript("", "<script>alert('信息填写不完全!')</script>"); return; } if (!txtValid.Text.ToUpper().Equals(Session["ValidNums"])) { Page.RegisterStartupScript("", "<script>alert('验证码不正确!')</script>"); return; } SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString); sql.Open(); string select = "select * from tb_user t where t.username = '" + txtName.Text.Trim() + "' and pwd = '" + txtPwd.Text.Trim() + "'"; SqlCommand command = new SqlCommand(select, sql); SqlDataReader dataReader = command.ExecuteReader(); if (dataReader.Read()) { //成功就跳转 Response.Redirect("Default.aspx?Name=" + txtName.Text + ""); } else { Page.RegisterStartupScript("", "<script>alert('账户名或密码错误!')</script>"); dataReader.Close(); return; } |
登录效果如图
修改密码介绍
首先建立一个EditPwd.aspx窗体
<table class="table" border="1px" align="center"> <tr> <td class="firstTd">用户名:</td> <td > <asp:DropDownList runat="server" ID="names" Width="200px" Height="20px" /> </td> </tr> <tr> <td class="firstTd">原密码:</td> <td > <asp:TextBox runat="server" ID="txtOldPwd" TextMode="Password" /> </td> </tr> <tr> <td class="firstTd">新密码:</td> <td > <asp:TextBox runat="server" ID="txtNewPwd" TextMode="Password"></asp:TextBox> </td> </tr> <tr> <td class="firstTd"> </td> <td align="right"> <span > <asp:Button runat="server" ID="btnSure" OnClick="btnSure_Click" Text="确认登录"/> <asp:Button runat="server" ID="btnCancle" OnClick="btnCancle_Click" Text="取消"/> </span> </td> </tr> </table> |
然后编写修改方法,包含SqlDataAdapter + DataSet关键点
protected void Page_Load(object sender, EventArgs e) { //初始化数据 if (!IsPostBack) { SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString); sql.Open(); string select = "select * from tb_user"; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(select, sql); DataSet dataSet = new DataSet(); sqlDataAdapter.Fill(dataSet); sql.Close(); if (dataSet.Tables[0].Rows.Count> 0) { for (int index = 0; index < dataSet.Tables[0].Rows.Count; index++) { names.Items.Add(dataSet.Tables[0].Rows[index][1].ToString()); } } } } protected void btnSure_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtNewPwd.Text) || string.IsNullOrEmpty(txtOldPwd.Text)) { Page.RegisterStartupScript("", "<script>alert('密码不能为空或者不能不相等!')</script>"); return; } SqlConnection sqlConnection = new SqlConnection("server=PC-20150424DMHQ;database=MyDatas;uid=sa;pwd=123456"); string select = "select * from tb_user where username = '" +names.Text + "'"; SqlCommand sqlCommand = new SqlCommand(select, sqlConnection); sqlConnection.Open(); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.Read()) { if (sqlDataReader["pwd"].ToString() != txtOldPwd.Text) { Page.RegisterStartupScript("", "<script>alert('密码输入错误!')</script>"); return; } } else { Page.RegisterStartupScript("", "<script>alert('数据库连接错误!')</script>"); return; } sqlConnection.Close(); sqlDataReader.Close(); //修改密码 sqlConnection.Open(); string updatePwd = "update tb_user set pwd = '" + txtNewPwd.Text + "' where username = '" + names.Text + "'"; sqlCommand = new SqlCommand(updatePwd, sqlConnection); sqlCommand.ExecuteNonQuery(); sqlConnection.Close(); Page.RegisterStartupScript("", "<script>alert('修改成功!')</script>"); Page_Load(null, null); } |
修改密码界面效果
【福利】填问卷 送2019精选测试大礼包+接口测试实战课程!