简体   繁体   English

未从下拉框中获取选定值

[英]Not getting selected value from drop down box

I'm having an issue getting the correct value from my drop down box. 我从下拉框中获取正确的值时遇到问题。 Basically what I have is a program with a drop down list that is populated with names that are saved in a database table. 基本上我所拥有的是一个带有下拉列表的程序,该列表中填充了保存在数据库表中的名称。 The user selects a name from the drop down box and then clicks a button to remove the name from the database. 用户从下拉框中选择一个名称,然后单击一个按钮从数据库中删除该名称。 The issue I am having is that no matter which name is selected, the first name (with an index of 0) is deleted from the drop down box. 我遇到的问题是,无论选择哪个名称,都会从下拉框中删除名字(索引为0)。

This leads me to believe that the SelectedIndex is reset when the button is clicked, which I don't understand. 这让我相信单击按钮时会重置SelectedIndex,这是我不明白的。

Code of delete button: 删除代码按钮:

protected void cmdDelete_Click(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(@"Server=myname-PC\SQLEXPRESS;Database=dbNames;Trusted_Connection=True;"))
    {
        try
        {
            conn.Open();
            SqlCommand comm = new SqlCommand("DELETE FROM Names WHERE FirstName=@FirstName AND LastName=@LastName", conn);
            char[] delims = new char[1];
            delims[0] = ' ';

            //lblGreeting.Text = cblNames.

            lblGreeting.Text = ddlNames.SelectedItem.Text;
            string[] names = ddlNames.SelectedValue.ToString().Split(delims);
            string fname = names[0];
            string lname = names[1];
            comm.Parameters.Add(new SqlParameter("@FirstName", fname));
            comm.Parameters.Add(new SqlParameter("@LastName", lname));
            comm.ExecuteNonQuery();
            conn.Close();
            LoadTable();
        }
        catch (Exception ex)
        {
            Response.Write("Delete Table: "+ex.ToString());
        }
    }
}

Any help is appreciated. 任何帮助表示赞赏。

I'm not really sure what should change if the page is not being rendered for the first time or why that would change what I want that function to do. 我不确定如果第一次没有呈现页面应该改变什么,或者为什么这会改变我想要的功能。 It is being called after the button is clicked, and the value should be taken before the table is rendered again. 在单击按钮后调用它,并且应该在再次呈现表之前获取值。 I'll include LoadTable() in case it helps: 我将包括LoadTable()以防它有帮助:

protected void LoadTable() 
{
    using (SqlConnection conn = new SqlConnection(@"Server=myname-PC\SQLEXPRESS;Database=dbNames;Trusted_Connection=True;"))
    {
        try
        {
            conn.Open();
            using (SqlCommand comm = new SqlCommand("SELECT * FROM Names",conn))
            {
                SqlDataReader rd = comm.ExecuteReader();
                string fname="";
                string lname="";
                ddlNames.Items.Clear();
                while (rd.Read())
                {
                    fname = rd.GetString(0);
                    lname = rd.GetString(1);
                    ddlNames.Items.Add(fname + " " + lname);
                }
                rd.Close();
            }                    
        }
        catch (Exception ex)
        {
            Response.Write("Load Table: "+ex.ToString());
        }
        conn.Close();
    }
}

Two other functions: 另外两个功能:

protected void Page_Load(object sender, EventArgs e)
{
    LoadTable();

}

protected void cmdAdd_Click(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(@"Server=myname-PC\SQLEXPRESS;Database=dbNames;Trusted_Connection=True;"))
    {
        try
        {
            conn.Open();
            char[] splits = new char[1];
            splits[0] = ' ';
            string fname = txtName.Text.Split(splits)[0];
            string lname = txtName.Text.Split(splits)[1];
            SqlCommand comm = new SqlCommand("INSERT INTO Names VALUES(@FirstName,@LastName)",conn);
            comm.Parameters.Add("@FirstName", fname);
            comm.Parameters.Add("@LastName", lname);
            comm.ExecuteNonQuery();
            conn.Close();
            LoadTable();

        }
        catch(Exception ex)
        {
            Response.Write("Add Name: "+ex.ToString());
        }
    }
}

You are possibly populating the dropdown in the Page_Load function and you do not have a Page.IsPostBack check. 您可能正在填充Page_Load函数中的下拉列表,并且您没有Page.IsPostBack检查。 Thus everytime the list is populated again. 因此,每次再次填充列表时。 Change the population code to something like: 将人口代码更改为:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadTable();
        }


    }

请尝试使用ddlNames.SelectedItem.Value。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM