Finally, a SelectedItems Option (yaaay)

If you're like me you've often pondered why controls like CheckBoxList don't provide a way to get a collection of selected items.  You've always had the option to iterate the Items collection and evaluate the Selected property, but why should that have to be done over and over again?  I want a quick way to get the selected items from a CheckBoxList, ListBox, DropDownList, and RadioButtonList; all items which inherit from ListControl.

The excellent creation of extension methods makes it simple to get the selected items from a ListControl.

using System.Collections.Generic;

using System.Web.UI.WebControls;

 

namespace ListControlExtensions

{

    public static class ListExtensions

    {

        public static List<ListItem> SelectedItems(this ListControl list)

        {

            List<ListItem> items = new List<ListItem>();

            foreach (ListItem item in list.Items)

            {

                if (item.Selected)

                {

                    items.Add(item);

                }

            }

            return items;

        }

    }

}

 

Have you found an alternative to getting the selected items from a ListControl without looping through the Items collection?

kick it on DotNetKicks.com

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Related posts

Comments

May 14. 2008 08:39 AM

Tom Lynch

Couldn't you simplify it like this?

public static List<ListItem> SelectedItems(this ListControl list)

Tom Lynch

May 14. 2008 09:40 AM

Ian Suttle

@Tom - Yes, you're absolutely correct. Thanks for pointing that out! That's what I get for posting late at night Smile.

Ian Suttle

May 14. 2008 11:41 AM

Mark S. Rasmussen

I'd much prefer simply using a LINQ query. It's more readable and architecturally simpler:

myListControl.Items.Where(li => li.Selected)

Mark S. Rasmussen

May 14. 2008 05:20 PM

Ian Suttle

@Mark - Definitely another alternative. I've ran some performance tests on this sort of thing in the past and found them to be ~1:1. Thanks for the comment.

Ian Suttle

May 15. 2008 03:13 AM

Janko

Nice usage of extension methods.

Janko

May 15. 2008 09:10 AM

Mark S. Rasmussen

Performance is not a concern in regards to this issue. What I'm saying is that I don't like extension methods for things like these. They clutter up the code - makes it tough for new developers to read the code. While they do have their places, imho, this is not one of them.

In this example I'd prefer the linq query as everyone can easily read from the code what is happening, and we're not creating any clutter. If linq is to be avoided, I'd make a static helper class that returned the list of selected items, given a ListControl (what the extension method does behind the covers, basically).

Mark S. Rasmussen

May 15. 2008 09:17 AM

Ian Suttle

@Mark - Thanks for sharing your thoughts on the matter.

Ian Suttle

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

August 21. 2008 12:26 PM

About Me

I'm Ian Suttle and I work for IGN Entertainment, a division of Fox Interactive Media.

Recent posts