我有列表框控件,我将值绑定到列表框
DataSet ds = new DataSet();
da.Fill(ds, "Employees");
ListBox1.DataSource = ds.Tables["Employees"].DefaultView;
ListBox1.SelectedIndex = 0;
ListBox1.DataTextField = "FirstName";
ListBox1.DataValueField = "StaffID";
ListBox1.Databind();
选择值后。 一旦用户点击此按钮,我就会有按钮
protected void BtnClick(object sender, EventArgs e)
{
var selectedItems = (from item in listBox.Items.Cast<ListItem>() where item.Selected select item.Text).ToArray();
result.Text = "You selected: ";
result.Text += string.Join(",", selectedItems);
}
现在我在事件内部代码给我选择的vlaues名称。 但我想获得所选的值索引 前
ID NAME
23 ram
34 tom
67 john
所以,如果我选择ram和john 在后面的代码中,我需要获得这些值为23,67的vales ID
希望我的问题很清楚
谢谢
答案 0 :(得分:0)
要做出这项工作需要做一些小改动
protected void BtnClick(object sender, EventArgs e)
{
var selectedItems = (from item in listBox.Items.Cast() where item.Selected select **item.Value**).ToArray();
result.Text = "You selected: ";
result.Text += string.Join(",", selectedItems);
}
这一行
var selectedItems = (from item in listBox.Items.Cast() where item.Selected select item.Value).ToArray();
答案 1 :(得分:0)
您需要迭代列表框项目,例如..
foreach (ListItem lstItem in lstProduct.Items)
{
if (lstItem.Selected)
{
}
}