Lee Kelleher’s Weblog

random posts on code, .NET, Umbraco and WordPress

Posts Tagged ‘calendar

Populating multiple DropDownList controls with generic ListItem array

with 2 comments

I’ve just had some fun spending the last half-an-hour trying to figure out why when I used the SelectedValue property of a DropDownList, it also set the value of another DropDownList control.

Here’s some background to the problem.  On my web-form, I have 2 fieldsets, one for a “Start Date”, the other for an “End Date”.  For each fieldset there are 3 DropDownList; Day, Month and Year.

Now rather than populating the values declaratively, using <asp:ListItem>; since the year values will need to be incremented annually. I opted to do this programmatically in the code-behind.

Here was my code (for the Day DropDownList):

List<ListItem> days = new List<ListItem>(32);
days.Add(new ListItem("Day", "-1"));
for (int i = 1; i <= 31; i++)
	days.Add(new ListItem(i.ToString(), i.ToString()));

// start date
ddlStartDateDay.Items.Clear();
ddlStartDateDay.Items.AddRange(days.ToArray());

// end date
ddlEndDateDay.Items.Clear();
ddlEndDateDay.Items.AddRange(days.ToArray());

So, whenever I tried to set the value of ddlStartDateDay.SelectedValue, the value of ddlEndDateDay would also change. So frustrated!

What I soon realised that when I was adding new ListItem objects to the List<ListItem>, it was creating a unique (internal) ID for each ListItem. Therefore when I was selecting the value for one DropDownList, it was selecting it across all DropDownList controls that contained that ListItem!

I’ve refactored my code to the following:

ddlStartDateDay.Items.Clear();
ddlEndDateDay.Items.Clear();

ddlStartDateDay.Items.Add(new ListItem("Day", "-1"));
ddlEndDateDay.Items.Add(new ListItem("Day", "-1"));

for (int i = 1; i <= 31; i++)
{
	ddlStartDateDay.Items.Add(new ListItem(i.ToString(), i.ToString()));
	ddlEndDateDay.Items.Add(new ListItem(i.ToString(), i.ToString()));
}

I’m not sure if there is any performance difference with this approach, I was just trying to use a single generic array (of ListItem) to populate multiple DropDownList controls. Obviously, this has it’s own drawbacks.

Written by Lee Kelleher

November 24, 2008 at 12:19 pm

Mozilla Prism – Bringing Web Apps to the Desktop

leave a comment »

I feel like I’ve been living under a rock for the last couple of months.  I’ve only just heard about Mozilla’s Prism – and it’s already changing the way I use web-apps.

Prism, (previously called WebRunner), is essentially a Site Specific Browser (SSB) – meaning that it’s a desktop application designed to host a single web-application.  This is good for many reasons, foremost it causes less distractions.

So far, I have prisms set-up for most of the Google apps that I regularly use: Google Mail, Google Calendar and Google Reader.   Now each of these web-applications are not open as separate tabs in my Firefox, but as individual desktop applications.  (Now I don’t have to worry about finding my Gmail tab in Firefox, nor about browser-crashes.)

It reminds of Microsoft attempted to do with HTA – but it seemed more difficult to interface them with external web-applications.

You can read more about Mozilla Prism on their Lab’s blog. [http://labs.mozilla.com/2007/10/prism/]

Written by Lee Kelleher

February 1, 2008 at 1:39 pm