我的网页中有多个下拉列表和列表框。
我正在尝试CategoryID
从lstCatID
列表框中获取列表,我可以使用类别名称填充列表框。
如果我在第一次尝试时没记错,我的代码运行良好,之后我做了一些更改,然后它声明总是选择第一个项目 x 时间
<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);
}
我不确定出了什么问题,我检查了 MSDN,它显示了完全相同的方法。
可能是我做错了什么。
只是使用 Firefox 添加我能够看到多个选定的值具有选定的属性。
<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
我将不胜感激这方面的帮助