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

Windows Live Writer And BlogEngine.Net

If you're a blogger you've probably heard of Windows Live Writer.  If you're not, please enjoy some of the other posts on my blog :).

Windows Live Writer has been raved over on many a blog.  I didn't think it'd work with BlogEngine.Net since it's not listed but alas I was wrong (again).

First impressions require two thumbs up.  Writer knows about my blog, how to log in to it, how to edit and publish posts and page, knows my tags, knows my categories, and more.  Very impressive.

I must say this couldn't have been so awesome without the BlogEngine.Net team implementing the communication points.

Windows Live Writer

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posting data with C#

I don't know why but I've had some trouble executing a POST request using C# server side in the past.  I guess since I don't have a need for posting data from the server often (or a compelling enough need to figure it out), I never did the homework.  Well, I did have that need today and proved to myself it was as simple as I'd expect.  The key points to remember are settings the ContentType to "application/x-www-form-urlencoded", setting the ContentLength to the length of the data you're posting, settings the Method to "Post", and writing the data to the RequestStream.  Here's an example:

HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;

req.ContentType = "application/x-www-form-urlencoded";

req.Method = "POST";

 

//write the post values

string postParams = "name=some sort of value";

byte[] buffer = System.Text.ASCIIEncoding.UTF8.GetBytes(postParams);

req.ContentLength = buffer.Length;

using (Stream writer = req.GetRequestStream())

{

    writer.Write(buffer, 0, buffer.Length);

}

 

//get the response

string responseData = string.Empty;

HttpWebResponse resp = req.GetResponse() as HttpWebResponse;

using (Stream receiveStream = resp.GetResponseStream())

{

    Encoding encode = Encoding.GetEncoding("utf-8");

    using (StreamReader readStream = new StreamReader(receiveStream, encode))

    {

        responseData = readStream.ReadToEnd();

    }

}

resp.Close();

I hope that's helpful to another!

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

About Me

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

Recent posts