Cannot have multiple items selected in a DropDownList

January 13, 2011

This error caused me at least an hour of research and nothing really helped. I found this error in an application I was building to test out some features in a Web Service I’d created. I was building a couple of ASP.Net DropDownLists to hold Day, Minute, and Hour values and just threw all of those items into a loop. Here is the code I was using:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 0; i < 60; i++)
{
ListItem li = new ListItem(i.ToString("00");
DeliveryMinute.Items.Add(li);

if (i < 24)
DeliveryHour.Items.Add(li);

if (i >= 1 && i < 32)
{
if (i == DateTime.Now.Day)
{
li.Selected = true;
DeliveryDay.Items.Add(li);
}
else
DeliveryDay.Items.Add(li);
}
}

SetListWithMonths();
DeliveryYear.Text = DateTime.Now.Year.ToString();
DeliveryHour.SelectedIndex = 0;
DeliveryMinute.SelectedIndex = 0;

}
}

Unfortunately, this code failed miserably. I could not get anything but the first item in the list to be selected. So instead I decided that the selected index would have to be set as one of the last items in the Page_Load event. I then added this before the end of the postback conditional:
DeliveryDay.SelectedIndex = DateTime.Now.Day - 1;
That is when the errors started occurring. I tried to clear the selection using DeliveryDay.ClearSelection(); to give me the desired result of clearing the selected item before I set the index of the item I wanted. This also generated the same errors. I continued to try to resolve this issue by using the FindByText() and FindByValue() methods and even tried to change my list item values by adding the date to them thinking there might be something wrong with the value and the text being the same. Of course that was not the case.
Eventually I started thinking about the ListItem object. I realized that I was adding the same object to each of DropDownList's ItemCollection object. I finally realized that this was my error. So I changed my code to this:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 0; i < 60; i++)
{

DeliveryMinute.Items.Add(i.ToString("00"));

if (i < 24)
DeliveryHour.Items.Add(i.ToString("00"));

if (i >= 1 && i < 32)
{
if (i == DateTime.Now.Day)
{
ListItem li = new ListItem(i.ToString());
li.Selected = true;
DeliveryDay.Items.Add(li);
}
else
DeliveryDay.Items.Add(i.ToString());
}
}

SetListWithMonths();
DeliveryYear.Text = DateTime.Now.Year.ToString();
DeliveryHour.SelectedIndex = 0;
DeliveryMinute.SelectedIndex = 0;
}
}

Now, each list contains their own ListItem object and there can be no sharing of the object between the different lists. I had mistakenly believed that each ListItem added to the collection would be passed by Value and that setting the Selected list item in one list would not affect the selected item in another list. After the page loads this seems to be the case but when initializing the page content using the same list item it can cause a world of grief.
I hope this post saves someone else a bunch of time because the information is invaluable to me!

Written by Bert Hileman - Visit Website

No related posts.

Leave a Reply




Spam Protection by WP-SpamFree