列表框中不能 select 多个项目
Cannot select multiple items in Listbox
我在 GridView
EditItemTemplate
中有一个 ListBox
和 DropDownList
。
在 rowEditing
,我想 select ListBox
中的所有项目。
下面是我的代码:
string categories = (e.Row.FindControl("lblCategoryID") as Label).Text;
ListBox lbx = (ListBox)e.Row.FindControl("lbSelectedCategory");
DropDownList drp = (DropDownList)e.Row.FindControl("drpCat");
List<string> TagIds = categories.Split(',').ToList();
lbx.SelectionMode = ListSelectionMode.Multiple;
foreach (string s in TagIds)
{
ListItem li = drp.Items.FindByValue(s);
lbx.Items.Add(li);
}
这里的项目被添加到 Listbox
。
但是当我尝试 select 多个项目时,
for (int i = 0; i < lbx.Items.Count; i++)
{
lbx.Items[i].Selected = true;
}
我收到错误:
cannot select multiple items in a dropdownlist.
Aspx:
<EditItemTemplate>
<asp:Label ID="lblCategoryID" Text='<%# Bind("tag_id") %>' runat="server"></asp:Label>
<asp:ListBox ID="lbSelectedCategory" Visible="false" Width="150px" runat="server"
SelectionMode="Multiple" CssClass="chosen-select"></asp:ListBox>
<asp:DropDownList ID="drpCat" AutoPostBack="true"
OnSelectedIndexChanged="drpCat_SelectedIndexChanged" CssClass="chosen-select" runat="server">
</asp:DropDownList>
</EditItemTemplate>
谁能帮我解决这个问题?
不要在下拉列表中查找行并将相同的行添加到 ListBox,而是尝试创建一个与下拉列表中的 ListItem 具有相同值的新 ListItem 并将其添加。
我在 GridView
EditItemTemplate
中有一个 ListBox
和 DropDownList
。
在 rowEditing
,我想 select ListBox
中的所有项目。
下面是我的代码:
string categories = (e.Row.FindControl("lblCategoryID") as Label).Text;
ListBox lbx = (ListBox)e.Row.FindControl("lbSelectedCategory");
DropDownList drp = (DropDownList)e.Row.FindControl("drpCat");
List<string> TagIds = categories.Split(',').ToList();
lbx.SelectionMode = ListSelectionMode.Multiple;
foreach (string s in TagIds)
{
ListItem li = drp.Items.FindByValue(s);
lbx.Items.Add(li);
}
这里的项目被添加到 Listbox
。
但是当我尝试 select 多个项目时,
for (int i = 0; i < lbx.Items.Count; i++)
{
lbx.Items[i].Selected = true;
}
我收到错误:
cannot select multiple items in a dropdownlist.
Aspx:
<EditItemTemplate>
<asp:Label ID="lblCategoryID" Text='<%# Bind("tag_id") %>' runat="server"></asp:Label>
<asp:ListBox ID="lbSelectedCategory" Visible="false" Width="150px" runat="server"
SelectionMode="Multiple" CssClass="chosen-select"></asp:ListBox>
<asp:DropDownList ID="drpCat" AutoPostBack="true"
OnSelectedIndexChanged="drpCat_SelectedIndexChanged" CssClass="chosen-select" runat="server">
</asp:DropDownList>
</EditItemTemplate>
谁能帮我解决这个问题?
不要在下拉列表中查找行并将相同的行添加到 ListBox,而是尝试创建一个与下拉列表中的 ListItem 具有相同值的新 ListItem 并将其添加。