ASP.NET菜鸟之路之登录系统
作者:网络转载 发布时间:[ 2015/7/6 14:49:03 ] 推荐标签:测试开发技术 .NET
登录介绍
首先添加登录窗口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);
}
修改密码界面效果
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11