【问题标题】:Multi-Select ListBox - Methods Do Not Seem To Work多选列表框 - 方法似乎不起作用
【发布时间】:2011-04-18 14:23:31
【问题描述】:

我正在使用 C# 2.0 - VS 2005 最新 SP。

问题是即使 ListBox SelectionMode 设置为多个,我也无法从 UserGroup 框中捕获第二个项目。我附上了处理列表框的按钮。我已经包含了我试图解决这个问题的两段代码。这是在 cmets 中标记的。

我将第一个选定项目设为真,但第二个选定项目为假。因此,将所选值添加到 Grp 对象永远不会发生。

 <asp:ListBox ID="UserGroup" Rows="5" runat="server" SelectionMode="multiple" CssClass="txtbox"></asp:ListBox>

代码在这里:

  protected void MapStudentGroup_OnClick(object sender, EventArgs e)
    {
        ListBox lstGroup = this.FindControl("UserGroup") as ListBox;
        ListBox lstStudent = this.FindControl("lbStudent") as ListBox;
        List<Group> Grps = new List<Group>();

        if (lstGroup.SelectedIndex != -1 && lstStudent.SelectedIndex != -1)
        {
            UserGroup usrGrp = new UserGroup();
            usrGrp.Id = Convert.ToInt32(lstStudent.SelectedValue);
            // get selected groups....

            // 1st Method tried here: 
            foreach (ListItem itm in lstGroup.Items)
            {
                if (itm.Selected == true)
                {
                    Group grp = new Group();
                    grp.GroupId = Convert.ToInt32(itm.Value);
                    Grps.Add(grp);
                }
            }

            // 2nd method tried.
            for (int i = 0; i < lstGroup.Items.Count; ++i)
            {
                if (lstGroup.Items[i].Selected == true)
                {
                    Group grp = new Group();
                    grp.GroupId = Convert.ToInt32(lstGroup.Items[i].Value);
                    Grps.Add(grp);
                }
            }

            // 3rd attempt : multiple selected items still not seen - Firefox Issue? 
            List<ListItem> selectedItems = new List<ListItem>();
            int[] selectedItemsIndexes = lstGroup.GetSelectedIndices();
            foreach (int selectedItem in selectedItemsIndexes)
            {
                //selectedItems.Add(lstGroup.Items[selectedItem]);
                Group grp = new Group();
                grp.GroupId = Convert.ToInt32(lstGroup.Items[selectedItem].Value);
                Grps.Add(grp);
            }

            usrGrp.UserGroups = Grps;
            // update group-user mappings...
            usrGrp.UpdateUserGroups(usrGrp);
        }
    }

【问题讨论】:

  • 什么是“lbStudent”?您是否从两个 ListBox 中选择了项目?
  • 这是一个检查学生是否被选中。在数据库中,我将 Student 和 Group 关联到一个 n-m 表映射中。
  • 尽管选择了一个项目,但 lst.Student.SelectedIndex 是 -1?
  • @Jan-Frederik Carl - 好吧,Student / Group 这两个控件不会相互加载。虽然我可以编写代码来隐藏此页面上的项目,直到选择学生,但这应该是一个快速/肮脏的管理页面,以方便为客户映射多对多表。由于两个列表框都是从不同的表中填充的,因此我试图防止客户端在尝试将一对项目映射到多对多表之前没有选择一个或其他框的情况。

标签: c# asp.net listbox


【解决方案1】:

我实际上会先通过以下方式简化事情:

    foreach (object itm in lstGroup.SelectedItems)
    {
         Group grp = new Group();
         grp.GroupId = Convert.ToInt32(itm);
         Grps.Add(grp);
    }

这样代码会稍微干净一些。对此进行测试,我能够显示我的所有选择。

由于你没有真正提到调试,如果上面的解决方案对你不起作用,我会在foreach结束后立即下一个断点,然后查看Grps的内容。如果Grps 中有正确数量的项目,那么问题就在您的代码中。也许是在usrGrp.UpdateUserGroups() 方法中。

我真的不明白为什么 Grps 里面没有正确数量的项目,但如果没有,请告诉我们,我相信我们可以解决。

更新:我尝试了您的代码的精简版本。它适用于我。对此进行测试,然后看看您是否可以将其重新构建以执行您需要它执行的操作。

我已经定义了一个简单的列表来存储选择:

List<int> selectionList = new List<int>();

然后,我使用您的通用框架填充它,除了我直接调用控件,因为它已经存在于当前页面上,而不是依赖于 this.FindControl()。

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (ListItem itm in UserGroup.Items)
    {
        if (itm.Selected == true)
        {
            selectionList.Add(Convert.ToInt32(itm.Value));
        }
    }
}

按下此按钮后,如果我选择了 3 个项目,则 selectionList 中有 3 个项目。希望你有同样的结果。祝你好运!

【讨论】:

  • 这些是 ASP.NET webforms,而不是 windowsforms。 Listbox 2.0 中没有定义 SelectedItems,或者至少我在对象浏览器中看到的是这样。还是谢谢你。我很感激。
  • @ericm 对于 webforms 与 winforms 的错误,我深表歉意。我的答案的后半部分呢?调试结果如何?
  • 对不起,我没有早点回来。我已经调试了所有三个选项,使用 ListItm 的第一个选项只看到选择的第一个项目。使用 Items.Count 的第二段代码仅返回选择的第一个项目。方法 3,再次只返回一个选定的索引。这对我来说是一个小错误,我把它放在了次要位置。我要从页面中删除控件并在今天重新创建并重新测试。
  • 感谢大家的帮助。事实证明,这是 DAL 中存储过程的问题。除了一个地方,桌子都被更换了。那张旧表对我的 id 有多个值。谢谢大家的建议。这是一个很棒的社区。非常感谢您的耐心。
  • @ericm 没问题。很高兴听到你想通了。您应该以某种形式发布您的解决方案作为答案并将其标记为已回答,这样这个问题就不会一直存在为未解决。
猜你喜欢
  • 2012-08-25
  • 1970-01-01
  • 2023-02-04
  • 2022-12-11
  • 2021-11-27
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多