【发布时间】:2015-10-02 14:05:38
【问题描述】:
我在GridView EditItemTemplate 中有一个ListBox 和DropDownList。
在rowEditing 上,我想选择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。
但是当我尝试选择多个项目时,
for (int i = 0; i < lbx.Items.Count; i++)
{
lbx.Items[i].Selected = true;
}
我得到错误:
不能在下拉列表中选择多个项目。
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>
谁能帮我解决这个问题?
【问题讨论】:
标签: c# asp.net gridview listbox