多个列表框项目未被选中的问题 [英] Problem of multiple listbox item not getting selected
本文介绍了多个列表框项目未被选中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我在VS 2008工作,我有一些问题与listbox listitem
i有一个列表框如
hello, am working in VS 2008 and i have some issue issue with listbox listitem
i have taken one listbox like
<asp:ListBox ID="lstcity" runat="server" class="form-control" SelectionMode="Multiple"></asp:ListBox>
和以编程方式添加的项目就像
and programatically added items to it like
SqlConnection conn = new SqlConnection(c.connStr);
string strQuery = "SELECT DistrictName,DistrictId FROM tbl_DistrictMaster";
SqlCommand cmd = new SqlCommand(strQuery, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
lstcity.Items.Clear();
lstcity.DataSource = dr;
lstcity.DataTextField = "DistrictName";
lstcity.DataValueField = "DistrictId";
lstcity.DataBind();
现在我想要选择的商品只显示
and now i want selected items only to show
string message = "";
foreach (ListItem item in lstcity.Items)
{
//sqll.Parameters.Clear();
//sqll.Parameters.AddWithValue("@City", lstcity.Text);
if (item.Selected == true)
{
message += item.Text + ",";
}
}
但即使它们在运行视图中显示也没有显示任何项目
请给我解决方案..
感谢adv
我尝试过:
but it is not showing any item selected even if they are displaying in run view
give me solution please..
thanks in adv
What I have tried:
string message = "";
foreach (ListItem item in lstcity.Items)
{
//sqll.Parameters.Clear();
//sqll.Parameters.AddWithValue("@City", lstcity.Text);
if (item.Selected == true)
{
message += item.Text + ",";
}
}
推荐答案
试试这个...
Hi,
Try this ...
string message = "";
foreach (var item in lstcity.SelectedItems)
{
message += item.ToString()+ ",";
}
这篇关于多个列表框项目未被选中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文