【问题标题】:Cannot select multiple items in Listbox无法在列表框中选择多个项目
【发布时间】:2015-10-02 14:05:38
【问题描述】:

我在GridView EditItemTemplate 中有一个ListBoxDropDownList。 在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


    【解决方案1】:

    不要在下拉列表中查找行并将相同的行添加到 ListBox,而是尝试创建一个与下拉列表中的 ListItem 具有相同值的新 ListItem,然后添加它。

    【讨论】:

    • 没有ListboxItem,只有Listitem。还有什么可以做的吗?感谢回复
    • 我可以选择一个选择一个项目。但不是多个。
    • @shanna ok 尝试使用 ListItems,但创建一个新项,而不是直接添加从下拉列表中找到的项。
    • @shanna 太棒了!编辑了我的答案以适合此解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多