我正在一个包含母版页的项目中。在该主页上,我有按钮(“主页”,“关于”)和“登录”下拉控件。登录后,我想在下拉列表控件中显示登录用户名并同时注销。
注册用户
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>
Home.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 =“ Logout”});这个可点击的.....
答案 0 :(得分:0)
您可以将其放在母版页的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" });
}