列表框在asp.net中没有得到所选项目 [英] Listbox in asp.net not getting selected items
问题描述
我有多个下拉&放大器;列表框在我的网页。
I have multiple dropdown & listbox in my webpage.
我想从一个 lstCatID
列表框,我能够填充列表框与类别id
列表类别名称。
I am trying to get a list of CategoryID
from a lstCatID
listbox i am able to populate the listbox with category name.
如果我在第一次尝试正确地记得我的code正常工作,在那之后我做了一些变化,然后它说总是得到时间的第一个项目选择了x号
If i remember correctly in first attempt my code worked fine, after that i made some change then it stated to always get the first item selected x No. of time
<asp:ListBox ID="lstCatID" runat="server" DataTextField="CategoryName"
DataValueField="CategoryID" SelectionMode="Multiple" CssClass="lstListBox">
</asp:ListBox>
protected void Button1_Click(object sender, EventArgs e)
{
string CatID = string.Empty;
foreach (ListItem li in lstCatID.Items)
{
if (li.Selected == true)
{
// Response.Write();
CatID += lstCatID.SelectedItem.Value + ",";
}
}
Response.Write(CatID);
}
我不知道是怎么回事错了,我checkd MSDN它准确显示做的是一样的。
I am not sure what is going wrong i checkd MSDN it show exactly the same way of doing it.
可能是我做错了什么。
只是为了用firefox,我能够看到多个选择的值选择添加属性。
Just to add using firefox i am able to see multiple selected value have selected property.
<option value="3" selected="selected">One</option>
<option value="2">Two</option>
<option value="29" selected="selected">Three</option>
<option value="25" selected="selected">Four</option>
<option value="22" >Five</option>
我在这种情况下输出将 3,3,3
我会在这方面AP preciate帮助
I would appreciate help in this regard
推荐答案
您是它每次设置为相同的值:
You are setting it to the same value every time:
foreach (ListItem li in lstCatID.Items)
{
if (li.Selected == true)
{
// you are always using lstCatID.SelectedItem.Value.
CatID += lstCatID.SelectedItem.Value + ",";
}
}
当你真正想要的是选择在循环项目的值:
When you actually want the value of the item in your loop that is selected:
foreach (ListItem li in lstCatID.Items)
{
if (li.Selected == true)
{
// get the value of the item in your loop
CatID += li.Value + ",";
}
}
这篇关于列表框在asp.net中没有得到所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!