开发者

Multi-Select ListBox - Methods Do Not Seem To Work

开发者 https://www.devze.com 2023-02-26 23:10 出处:网络
I\'m working with C# 2.0 - VS 2005 Latest SP\'s. The problem is I cannot capture the second items from the UserGroup box even though the ListBox SelectionMode is set to multiple.I have attached the

I'm working with C# 2.0 - VS 2005 Latest SP's.

The problem is I cannot capture the second items from the UserGroup box even though the ListBox SelectionMode is set to multiple. I have attached the button that processes the listbox. I've included the two segments of code that I've tried to resolve this issue. That is marked in the comments.

I'm getting the 1st selected item as true, but the second selected item is false. So the addition of the selected value to the Grp object never happens.

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

Code Here:

  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);
        }
    }


I would actually simplify things first by doing the following:

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

This way the code is slightly cleaner. Testing this, I am able to get all of my selections to show up.

Since you haven't really mentioned anything about debugging, if the above solution doesn't work for you, I would put a breakpoint immediately after the foreach ends and look at the contents of Grps. If Grps has the right amount of items in it, then the problem is further down in your code. Perhaps it is in usrGrp.UpdateUserGroups() method.

I don't really see why Grps wouldn't have the right number of items in it, but if it doesn't, let us know and I am sure we can figure something out.

Update: I tried a stripped down version of your code. It works properly for me. Test this out and then see if you can build it back up to do what you need it to do.

I've defined a simple list to store the selections:

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

Then, I populate it using your general framework, except I call the control directly since it already exists on the current page, rather than relying on 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));
        }
    }
}

After this button is pressed, if I've selected 3 items, then 3 items are in selectionList. Hopefully, you have the same result. Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消