c# - 如何在下拉列表中显示登录用户相关任务
问题描述
我正在开发一个包含母版页的项目。在该母版页上,我有按钮(主页、关于)和登录下拉控件。登录后,我想在下拉列表控件中显示登录用户名并同时注销。
注册用户
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spRegisterUser", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter Name = new SqlParameter("@Name", txtName.Text);
SqlParameter Email = new SqlParameter("@Email", txtEmail.Text);
string encryptedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "SHA1");
SqlParameter password = new SqlParameter("@Password", encryptedPassword);
SqlParameter Mobile = new SqlParameter("@MobileNo", txtMobile.Text);
cmd.Parameters.Add(Name);
cmd.Parameters.Add(Email);
cmd.Parameters.Add(password);
cmd.Parameters.Add(Mobile);
con.Open();
int ReturnCode = (int)cmd.ExecuteScalar();
if (ReturnCode == -1)
{
lblmessage.ForeColor = System.Drawing.Color.Red;
lblmessage.Text = "Email Already In Use";
}
else
{
SendEmail();
Response.Redirect("https://localhost/JustEat.com/Login.aspx");
}
}
Home.Master 下拉列表控件
<asp:DropDownList ID="ddlLogin" CssClass="btn" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="ddlLogin_SelectedIndexChanged">
<asp:ListItem Value="-1">User</asp:ListItem>
<asp:ListItem Value="1">SignUp</asp:ListItem>
<asp:ListItem Value="2">Login</asp:ListItem>
</asp:DropDownList>
主页.Master.aspx.cs
protected void ddlLogin_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlLogin.SelectedValue == "-1")
{
}
else if (ddlLogin.SelectedValue == "1")
{
Response.Redirect("https://localhost/JustEat.com/Signup.aspx");
}
else if (ddlLogin.SelectedValue == "2")
{
Response.Redirect("https://localhost/JustEat.com/Login.aspx");
}
}
验证用户的方法
private bool AuthenticateUser(string username, string password)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spAuthenticateUser", con);
cmd.CommandType = CommandType.StoredProcedure;
string EncryptedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
SqlParameter paramEmail = new SqlParameter("@Email", username);
SqlParameter paramPassword = new SqlParameter("@Password", EncryptedPassword);
cmd.Parameters.Add(paramEmail);
cmd.Parameters.Add(paramPassword);
con.Open();
int ReturnCode = (int)cmd.ExecuteScalar();
return ReturnCode == 1;
}
}
登录按钮点击事件
if (AuthenticateUser(txtEmail.Text, txtPassword.Text))
{
Session["UserName"] = txtEmail.Text;
Response.Redirect("https://localhost/JustEat.com/Home.aspx");
}
else
{
lblmessage.ForeColor = System.Drawing.Color.Red;
lblmessage.Text = "Invalid User Name and/or Password";
}
用户输入凭据后
if (Session["UserName"] != null)
{
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlDataAdapter da = new SqlDataAdapter("Select * from tblRegistration where Email='" + Session["UserName"] + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
ddlLogin.Items.Clear();
ddlLogin.Items.Add(ds.Tables[0].Rows[0]["Name"].ToString());
ddlLogin.Items.Add(new ListItem() { Value = "Log", Text = "Log out" });
}
}
现在的问题是我怎样才能使 ddlLogin.Items.Add(new ListItem() { Value = "Log", Text = "Log out" }); 这个可点击.....
解决方案
您可以将它放在母版页的 Page_Load 中。当请求通过身份验证并且没有 PostBack 时,它会清除 DropDownList 中的当前项目并添加新项目。
if (Request.IsAuthenticated && !IsPostBack)
{
ddlLogin.Items.Clear();
ddlLogin.Items.Add(new ListItem() { Value = "-1", Text = "UserName" });
ddlLogin.Items.Add(new ListItem() { Value = "1", Text = "Log out" });
}
推荐阅读
- android - 如何在 iOS 设备上设置仅查看的边距?
- javascript - 如何使用数组对象内的数组进行过滤
- xamarin.forms - 是否可以从模式中导航回来,然后在同一个导航操作中进一步返回?
- javascript - 如何将 JS 模块 (.mjs) 导入 QML
- llvm - 如何检查两个 GEP 指令在语义上是否相等?
- ignite - ignite 从 2.7 升级到 2.11 ,启动时出错
- javascript - 在浏览器上运行 JavaScript,并使用 R 和 Shiny 在服务器上处理结果
- c# - 在另一个启动 WPF 之前等待一个动画结束
- graphics - 金属缓冲区没有完全复制到 GPU
- php - 如何在 React-Native 中使用 sha256 生成 hash_hmac