We ran into the following issue two days ago.  In a nutshell, the “initialCategory” was not being selected in the Categories dropdown by default.  Yes, I recognize that the code itself may be written in fewer lines, but please only focus on the differences between the two code snippets below.  I haven’t had the time to research, but this code has been rolling around in my head ever since a developer presented the “fix” to me yesterday morning.  As far as I can tell, only syntax has changed.  I would expected the code to work in both case, to be honest, but the “before” code didn’t function correctly with our last release.

Any thoughts?

 

Before

for(int i=0;i<cboCategories.Items.Count;i++)
{
    if(initialCategory != string.Empty)
    {
        if(cboCategories.Items[i].Value.ToLower()==initialCategory.ToLower())
        {
            cboCategories.Items[i].Selected=true;
            break;
        }
    }
    else if(cboCategories.Items[i].Value==all)
    {
        cboCategories.Items[i].Selected=true;
        break;
    }
}

After

for(int i=0;i<cboCategories.Items.Count;i++)
{
    if(initialCategory != string.Empty)
    {
        if(cboCategories.Items[i].Value.ToLower()==initialCategory.ToLower())
        {
            //cboCategories.Items[i].Selected=true;
            cboCategories.SelectedIndex = i;
            break;
        }
    }
    else if(cboCategories.Items[i].Value==all)
    {
        //cboCategories.Items[i].Selected=true;
        cboCategories.SelectedIndex = i;
        break;
    }
}

3 Comments to “C# Brain Scratcher of the Day”

  1. NinjaCross says:

    As far as I can see, in the first case you are allowing to have multiple items with the “Selected” property to true.
    This is not a suitable working condition for a DropDownList, since only one of them is allowed to be true (infact you have the “int SelectedIndex” property, and one named “int[] SelectedIndexes” that returns a list of all the selected items in the DropDownList).

    I suppose it’s a sort of design-flaw inside the DropDownList class, cause the class is not aware about the consistency of its on selection state… so it’s not completely your fault ;)

  2. NinjaCross says:

    … ops !
    In my previous post i wrote “…and one named “int[] SelectedIndexes” that returns…”
    but obviously i meant
    “…and NOT one named “int[] SelectedIndexes” that returns…”

  3. bgriswold says:

    Good observation. We started down a similar path when debugging and found an exception was thrown when we attempted to set more than one item’s selected property to true.

    We dug a little deeper and found the issue wasn’t actually tied to change in the routine after all. The issue was resolved by another fix which was checked in at roughly the same time and moved related logic between page events. It took us too long to figure this one out — mostly because we were stuck on “how could this change possibly fix the problem” when in fact it didn’t.

    Thanks for the feedback.

Leave a Reply

You can wrap your code with [ruby][/ruby] or [python][/python] blocks for syntax highlighting and you can use these traditional tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>