首页 > 解决方案 > 如何在下拉列表中显示登录用户相关任务

问题描述

我正在开发一个包含母版页的项目。在该母版页上,我有按钮(主页、关于)和登录下拉控件。登录后,我想在下拉列表控件中显示登录用户名并同时注销。

注册用户

   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" }); 这个可点击.....

标签: c#asp.netsql-serverado.net

解决方案


您可以将它放在母版页的 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" });
}

推荐阅读