简体   繁体   English

下拉菜单的选定索引与显示的选定元素的位置不匹配

[英]Drop down's selected Index doesn't match the position of the displayed selected element

I have a dropdown list and an add button. 我有一个下拉列表和一个添加按钮。 When the add button is clicked, the selected item from the dropdown is supposed to be added to a database. 单击添加按钮后,将从下拉菜单中选择的项目添加到数据库中。 However the selectedIndex value used in the function called by the add button is wrong. 但是在添加按钮调用的函数中使用的selectedIndex值是错误的。 It doesn't match the actual index of the currently selected item (most of the time.) (大部分时间)它与当前所选项目的实际索引不匹配。

Here is the dropdown and the button: 这是下拉列表和按钮:

<asp:DropDownList runat="server" id="ddlNewCreature" AutoPostBack = "false" DataMember="0"></asp:DropDownList>

<asp:Button ID="btnAddCreature" runat="server" Text="Add" onClick="AddCreature"/>

Here is the code behind: 这是背后的代码:

protected void Page_Load(object sender, EventArgs e)
{                     
    if (!IsPostBack)
    {
    if (ddParent1.Items.Count == 0)
    {
        if (ddBaby.Items.Count == 0)
        {
        loadCreatureLists(true, true);
        }
        else
        {
        loadCreatureLists(true, false);
        }
    }
    else
    {
        if (ddBaby.Items.Count == 0)
        {
        loadCreatureLists(false, true);
        }
    }
    }
}


protected void loadCreatureLists(Boolean loadParents, Boolean loadOffspring )
{
    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Drin"].ConnectionString);
    string sSql = "select creature, element1, element2 from dbo.tinycastlecreatures order by creature";
    SqlCommand cmd = new SqlCommand(sSql, conn);
    SqlDataReader reader;

    try
    {
    conn.Open();
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        ListItem newItem = new ListItem();
        newItem.Text = reader["creature"].ToString();
        newItem.Value = normalizeElementOrder(reader["element1"].ToString(),reader["element2"].ToString());
        if (loadParents && !chkMine.Checked)
        {

        ddParent1.Items.Add(newItem);
        ddParent2.Items.Add(newItem);
        }
        if (loadOffspring)
        {
        ddBaby.Items.Add(newItem);
        ddlNewCreature.Items.Add(newItem);
        }
    }
    reader.Close();
    }
    catch (Exception err) { }
    finally { if (!loadParents || !chkMine.Checked) { conn.Close(); } }
}

protected void AddCreature(object sender, EventArgs e)
{
    int indx = ddlNewCreature.SelectedIndex;
    string sCreature = ddlNewCreature.Items[indx].Text.ToString();
    for (int i = 0; i < ddlNewCreature.Items.Count; i++ )
    {
    sCreature = ddlNewCreature.Items[i].Text.ToString();
    }
    ddlNewCreature.SelectedIndex = 0;
    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Drin"].ConnectionString);
    conn.Open();
    string sSql = "[dbo].[tinycastle_add_user_creature] '";
    sSql += sCreature;
    sSql += "','deb'"; 
    SqlCommand cmd = new SqlCommand(sSql, conn);
    cmd.ExecuteNonQuery();
    sCreature = ddlNewCreature.Items[indx].Text.ToString();
    sqlMineSource.DataBind();
    gridMine.DataBind();
}

B.Yaylaci's suggestion on checking the SelectedIndex in the dropdown's changed event is on the right track to find out why (when? :)) the index isn't matching up with what you expect, I think. 我认为B.Yaylaci建议在下拉列表的changed事件中检查SelectedIndex的建议是正确的,以找出为什么(何时?:)索引与您的期望不匹配。 Also, it'd be much easier (and more-accurate) to use the SelectedValue property if all you need is the text in the dropdown. 另外,如果您需要的只是下拉列表中的文本,则使用SelectedValue属性会容易得多(更准确)。 https://stackoverflow.com/a/4399747 https://stackoverflow.com/a/4399747

I circumvented the problem by ripping out the way the various drop down lists were populated, and binding them to a SQLDataStore instead. 我通过消除各种下拉列表的填充方式并将它们绑定到SQLDataStore来避免了这个问题。 I don't know why that helped, but it did. 我不知道为什么这样做有帮助,但确实有帮助。

Use ddlNewCreature.SelectedValue this will fix your problem." SelectedValue " give you the correct index value for the selected DropDownList. 使用ddlNewCreature.SelectedValue可以解决您的问题。“ SelectedValue”为选定的DropDownList提供正确的索引值。 This worked for me. 这对我有用。

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

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