C# listbox中的第一项总是被读取,而不是listbox中的选定(突出显示)项
让我在这里再试一次。。。ASP.NET webform中的“我的删除”功能,如下所示:C# listbox中的第一项总是被读取,而不是listbox中的选定(突出显示)项,c#,asp.net,webforms,listbox,viewstate,C#,Asp.net,Webforms,Listbox,Viewstate,让我在这里再试一次。。。ASP.NET webform中的“我的删除”功能,如下所示: //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
//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();
}
//从产品列表中删除产品
受保护的无效btn\u Del\u单击(对象发送者,事件参数e)
{
int index=lst_Products.SelectedIndex;//product.CategoryId==cID&&product.FullLine==pfulllline.ToList();
foreach(过滤产品中的产品产品删除)
{
//删除它。大多数情况下,这只会是一个项目,但可以有多个条目。
所有产品。移除(prodToDelete);
}
//存储修改后的产品列表
ViewState[“产品”]=所有产品;
//lst_Products.DataBind();
//BindProdData();
//展示产品
ShowProd();
//在产品ID文本框中显示cat ID(产品ID始终=类别ID)
txt_ProdID.Text=cID.ToString();
}
代码中的箭头显示了问题发生的位置:lst\u Products。SelectedIndex
给出的索引值错误,指向列表框中显示的第一个项目,lst\u Products
,它指向索引0,而不是索引1(列表框中突出显示的条目)。请看下图:
我是否误解了代码的SelectedIndex
部分的目的?我想应该读一下突出显示的项目。如果没有,如何读取列表框中的选定(突出显示)项
下面是我的代码的一小部分,其中包含调试数据以供比较:
具体地说,我用鼠标在列表框中选择索引1以突出显示它,然后点击删除按钮,但是索引0会被删除
因此,我的问题是:
谢谢您的帮助。您试过以下内容吗
DropDownList1.Items[DropDownList1.SelectedIndex].Value
我对Winform上的列表框也有同样的问题-列表中的第一项是自动选择的。我首先清除了选择列表(VB.NET代码): 然后,我根据在数据集中选择的项目在列表框中选择了项目:
'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
“检查数据集中的每一行
对于MyDataset.Tables(0.Rows)中的每个MembershipROW
'在列表框中选择此成员资格类型
MembershipType=MembershipROW.Item(“MembershipType”)
MembershipIndex=lboxMembershipTypes.FindStringExact(MembershipType)
“这是我们名单上的成员吗?
如果(MembershipIndex列表框.NoMatches),则
'是否选择了此成员资格?
如果(MembershipROW.Item(“SelectMembership”)),则
'选择此成员资格类型
lboxMembershipTypes.SetSelected(MembershipIndex,True)
如果结束
如果结束
下一个
我做了一个更一般的解决方案,而不是简单地检查第一个项目是否被选中,如果没有,则取消选中-但是,这也会起作用。我知道这是一个旧线程。但是2.5年后,我在google上遇到了同样的问题,但运气不好,我发现这个线程有着完全相同的问题(甚至是调试示例) 我通过使重复的listitem值唯一化来删除它们,从而解决了这个问题。我将这个列表框数据绑定到一个linq var,该linq var来自一个具有重复项的namevaluecollection(是的,我不想要字典,因为我想要按部门对成员进行分组,并且想要部门作为键) 这就是我为数据源所做的事情,也是我遇到的问题:
var members=nvc\u members.AllKeys.SelectMany(nvc\u members.GetValues,(k,v)=>new{key=k,value=v})
然后我会将键分配给值,将值分配给数据列表中的文本。
此更改使我在列表框中创建了唯一的值:
var members=nvc\u members.AllKeys.SelectMany(nvc\u members.GetValues,(k,v)=>new{key=k+“-”+v,value=v})
如果我刚开始使用字典并以同样的方式生成唯一键,这可能是可以避免的,但这样我就不会在这里结束。如何在列表框中添加该项?是数据源吗?或者手动添加诸如listbox.items.add(“一些数据”)之类的项;您是否在lst_产品中获得正确的值。SelectedValue?@CST它来自ViewState。这部分代码也在工作。你能分享listbox的aspx代码吗(愚蠢的阻塞我的答案)。基于对具有类似功能的不同解决方案代码的测试,我得出结论,
SelectedItem.Text
或SelectedValue
并不意味着listbox上显示的突出显示的条目。它只是表示列表中第一个遇到的具有选定值的条目,而不管它是否高亮显示。因此,数据结构必须为创建的每个条目包含唯一的ID。这保证了正确的条目将被删除。无论如何,这是一个列表框,而不是下拉列表。哦,很抱歉,我没有看到我将检查另一个解决方案,可能是如果我可以帮忙的话。很抱歉,我无法突出显示所有代码,因此请注意“for Each…”是代码部分的开头,“Next”底部是代码的结尾。
'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