【问题标题】:First item in listbox always gets read instead of selected (highlighted) item in listbox列表框中的第一项始终被读取,而不是列表框中的选定(突出显示)项
【发布时间】:2016-08-08 05:07:05
【问题描述】:

让我在这里再试一次...我在 ASP.NET 网络表单中的删除功能:

        //delete a product from the product list 
    protected void btn_Del_Click(object sender, EventArgs e)
    {
        int index = lst_Products.SelectedIndex;  //<=== this is the code that gets me stumped. 
                                                 //index keeps returning 0 (zero) whether postback or not

        //Store product cID and FullLine for LINQ compare
        string pFullLine = lst_Products.Items[index].ToString();
        int cID = Convert.ToInt32(lst_Products.Items[index].Value);

        // Read ViewState
        List<Product> allProducts = (List<Product>)ViewState["products"];

        List<Product> productsfiltered = allProducts
                                         .Where(product => product.CategoryId == cID && product.FullLine == pFullLine).ToList();

        foreach (Product prodToDelete in productsfiltered)
        {
            //delete it.  Most of the time this would only be one item, but more than one entry is possible.
            allProducts.Remove(prodToDelete);
        }

        //store modified product list
        ViewState["products"] = allProducts;


        //lst_Products.DataBind();
        //BindProdData();

        //display products
        ShowProd();

        //show cat ID in Product ID textbox (product ID always = Category ID)
        txt_ProdID.Text = cID.ToString();

    }

代码中的箭头显示了问题发生的位置:lst_Products.SelectedIndex 给出了错误的索引值,指向列表框中显示的第一项,lst_Products,它指向索引 0 而不是索引 1(在列表框)。看看下面这张图片:

我是否误解了代码中SelectedIndex 部分的用途?我认为它应该阅读突出显示的项目。如果没有,我该怎么做才能阅读列表框中选定的(突出显示的)项目?

下面是我的代码的一小部分,带有调试数据以供比较:

具体来说,我在列表框中用鼠标选择索引 1 以突出显示它,然后点击删除按钮,但索引 0 被删除了。

所以我的问题是:

  1. 尽管我在列表框中突出显示了索引 1,但为什么索引 0 “selected=true”?
  2. 如何选择索引 1(或索引 0 以外的索引),以便我的删除例程阻止它删除它?我正在尝试根据索引从列表框中导出文本字符串。
  3. 是否有更合适的代码来做同样的事情?

感谢您的帮助。

【问题讨论】:

  • 如何在列表框中添加项目?是数据源吗?或手动添加 listbox.items.add("some data"); 之类的项目
  • 你在 lst_Products.SelectedValue 中得到正确的值吗?
  • @CST 来自 ViewState。这部分代码也可以工作。
  • 可以分享listbox的aspx代码
  • (愚蠢地阻止了我的回答。)基于对具有类似功能的不同解决方案代码的测试,我得出的结论是SelectedItem.TextSelectedValue 确实不 表示列表框上显示的突出显示的条目。它只是表示列表中第一个遇到的具有所选值的条目,无论它是否突出显示。因此,数据结构必须为每个创建的条目包含一个唯一的 ID。这保证了正确的条目将被删除。

标签: c# asp.net webforms listbox viewstate


【解决方案1】:

你试过关注吗?

DropDownList1.Items[DropDownList1.SelectedIndex].Value

【讨论】:

  • 不管怎样,它是一个列表框,而不是一个下拉列表。
  • 哦,对不起,我没看到
  • 如果我能提供帮助,我会检查另一种解决方案
【解决方案2】:

Winform 上的列表框也有同样的问题 - 列表中的第一项是自动选择的。我先清除了选择列表(VB.NET代码):

lboxMembershipTypes.SelectedItems.Clear()

然后我根据我的数据集中选择的项目选择列表框中的项目:

'check each row in the dataset

For Each MembershipROW In MyDataset.Tables(0).Rows

'select this membership type in the listbox

MembershipType = MembershipROW.Item("MembershipType")

MembershipIndex = lboxMembershipTypes.FindStringExact(MembershipType)

'is this membership in our list?

If (MembershipIndex <> ListBox.NoMatches) Then

  'is this membership selected?

   If (MembershipROW.Item("SelectMembership")) Then

     'select this membership type

     lboxMembershipTypes.SetSelected(MembershipIndex, True)

   End If

 End If

Next

我做了一个更通用的解决方案,而不是简单地检查第一个项目是否被选中,如果没有,则取消选中它 - 但是,这也可以。

【讨论】:

  • 抱歉,我无法突出显示所有代码,因此请注意“For Each...”是代码部分的开头,底部的“Next”是代码结束。
【解决方案3】:

我知道这是一个旧线程。但 2.5 年后,我在 google 上遇到了同样的问题,运气不佳,发现这个线程有完全相同的问题(甚至到调试示例)。

我通过删除重复的 listitem 值来解决这个问题,方法是使它们唯一。我将此列表框数据绑定到来自具有重复项的 namevaluecollection 的 linq var(是的,我不想要字典,因为我想按部门对成员进行分组,并希望部门作为键)。

这就是我为数据源所做的事情并且遇到了问题: var members = nvc_members.AllKeys.SelectMany(nvc_members.GetValues, (k, v) =&gt; new { key = k, value = v }) 然后我会将键分配给数据列表中的值,并将值分配给数据列表中的文本。 此更改有助于我在列表框中创建唯一值: var members = nvc_members.AllKeys.SelectMany(nvc_members.GetValues, (k, v) =&gt; new { key = k+"-"+v, value = v })

如果我一开始就使用字典并以相同的方式创建唯一键,这可能可以避免,但我不会在这里结束。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多