I recently had to implement a pair of cascading DropDownLists for an ASP.NET Web project and found very little workable code samples on Google. Here's the code that worked for me.

The second DropDownList is enabled based on the selection made in the first and the cascade works in both directions, depending on the selection. The tricky bit was getting the index of the selection in the second drop down. I eventually found samples of the IndexOf and FindByText ListItemCollection methods on the MSDN site.

    <asp:DropDownList ID="InstrumentDDL" AutoPostBack="true"  
        OnSelectedIndexChanged="InstrumentDDL_SelectedIndexChanged"
            Width="100" runat="server">
        <asp:ListItem Text="Select" Value="Select"></asp:ListItem>
        <asp:ListItem Text="Guitar" Value="Guitar"></asp:ListItem>
        <asp:ListItem Text="Mandolin" Value="Mandolin"></asp:ListItem>
    </asp:DropDownList>

    <asp:DropDownList ID="GuitarDDL" AutoPostBack="true"  
        OnSelectedIndexChanged="GuitarDDL_SelectedIndexChanged"
            Enabled="false" Width="100" runat="server">
        <asp:ListItem Text="Select" Value="Select"></asp:ListItem>
        <asp:ListItem Text="Fender" Value="Fender"></asp:ListItem>
        <asp:ListItem Text="Gibson" Value="Gibson"></asp:ListItem>
        <asp:ListItem Text="Gretsch" Value="Gretsch"></asp:ListItem>
        <asp:ListItem Text="Martin" Value="Martin"></asp:ListItem>
    </asp:DropDownList>

    protected void InstrumentDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Reset GuitarDDL
        GuitarDDL.SelectedIndex = -1;

        // Dynamically enable the GuitarDDL based on InstrumentDDL selection
        if (InstrumentDDL.SelectedValue == "Guitar")
        {
            GuitarDDL.SelectedItem.Text = "Select";
            GuitarDDL.Enabled = true;
        }
        else
        {
            GuitarDDL.SelectedItem.Text = GuitarDDL.SelectedValue;
            GuitarDDL.Enabled = false;
        }
    }

    protected void GuitarDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Every menu item refers to "Guitar" in the InstrumentDDL menu (except "Select")
        if (GuitarDDL.SelectedValue == "Select")
        {
            // Reset InstrumentDDL menu and disable the GuitarDDL menu
            InstrumentDDL.SelectedIndex
                = InstrumentDDL.Items.IndexOf(InstrumentDDL.Items.FindByText("Select"));
            GuitarDDL.Enabled = false;
        }
        else
        {
            InstrumentDDL.SelectedIndex
                 = InstrumentDDL.Items.IndexOf(InstrumentDDL.Items.FindByText("Guitar"));
        }
    }

 


Welcome to CodersBarn.com

by agrace 10. June 2007 21:26

Here goes... I guess I've been gleaning the goods from other people's sites long enough! They say committing to a blog is a time-consuming venture. I've decided to get this site out fast and then add to it gradually. The main motivation behind this blog is to create a forum where developers like myself can discuss programming and design issues. I want to encourage beginners and experts alike to make this site a place where people can have fun and learn at the same time. 

I have been reading Mads Kristensen's blog and it is his Blog Engine that is under the hood. I have been using ASP.NET and C# for around three years. I'm no expert but was able to download Mads' Blog Engine this afternoon and have it up and running in no time. I made some minor changes to the theme stylesheet and that was it. The previous night, while browsing through iStockphoto I came across the banner picture you see above. This morning, I took one look at it and the domain name just jumped out at me. Five minutes later, I had it registered on GoDaddy and this baby was born!

This is a really exciting time for Web Developers in general. Hopefully, some of the subjects that interest me personally will be representative of some of the new technologies emerging in our industry as we look forward. Please email me if you have any thoughts about interesting directions this site could possibly take.

I am currently embarking on the Ajax and SharePoint learning curve but ASP.NET 2.0 is still the main focus of my work and future posts will probably reflect that.

Anthony Grace :-)

 

Tags: ,

ASP.NET | C#