背景
我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了。
网站介绍
根据书上的例子做了一个比较粗糙的登录例子,里面的代码都是自己敲出来的,而且很少使用封装方法,就是为了让自己能更清楚的记住做的过程。 这个网站包含注册、登录、修改密码三个功能。
注册介绍
新建一个Web窗体,即UserManagers.aspx。不粘贴前台代码了。 然后编写注册方法,包括用户名当作主键,SqlDataReader方式读取数据库,SqlCommand参数添加数据等要点protected void Button1_Click(object sender, EventArgs e) { if (txtName.Text == "" || txtPwd.Text == "" || txtConfirm.Text == "") { this.Page.RegisterStartupScript("ss", "<script>alert('用户名密码不能为空')</script>"); return; } if (txtPwd.Text.Equals(txtConfirm.Text)) { //查看当前用户是否存在 SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString); sqlConn.Open(); string sql = "select * from tb_user where username = '" + txtName.Text.Trim() + "'"; SqlCommand sqlCommand = new SqlCommand(sql, sqlConn); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.Read()) { Page.RegisterStartupScript("", "<script>alert('用户名已存在!')</script>"); return; } sqlDataReader.Close(); //新增用户 string strInsert = "insert into tb_user(username, pwd, marks) values (@username,@pwd, @marks)"; sqlCommand = new SqlCommand(strInsert, sqlConn); sqlCommand.Parameters.Add("@username", SqlDbType.VarChar); sqlCommand.Parameters["@username"].Value = txtName.Text; sqlCommand.Parameters.Add("@pwd", SqlDbType.VarChar, 20); sqlCommand.Parameters["@pwd"].Value = txtPwd.Text; sqlCommand.Parameters.Add("@marks", SqlDbType.VarChar, 1000); sqlCommand.Parameters["@marks"].Value = "zbq测试"; sqlCommand.ExecuteNonQuery(); sqlConn.Close(); Page.RegisterStartupScript("", "<script>alert('注册成功!')</script>"); Response.Redirect("Default.aspx?Name=" + txtName.Text + ""); } }
界面效果如下
登录介绍
首先添加登录窗口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; }
![image image](https://i-blog.csdnimg.cn/blog_migrate/027665c5cb4583e0311df4ef5bb6d7b8.png)
修改密码介绍
首先建立一个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); }
修改密码界面效果