Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
I created a custom Templated User Control, for the purpose of having a standard "container" to use around our web app, but allow whatever controls you want inside of it -- pretty much like a Template Column of a GridView/DataGrid. Problem I'm having is that when I place a DropDownList control inside my custom control, the SelectedIndexChanged method of that DDL doesn't always fire, and I am stumped as to why.
I created a small test page with my custom control and a single DDL, which has some hard coded values, to replicate the problem:
<uc1:ExpandCollapseRegion ID="xpcColor" runat="server" Title="Pick a Color" AlwaysOpen="true">
    <LayoutTemplate>
        <table>
            <tr>
                <td>Color:&nbsp;</td>
                <td>
                    <asp:DropDownList ID="ddlColor" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlColor_SelectedIndexChanged">
                        <asp:ListItem Text="" Value="" />
                        <asp:ListItem Text="Red" Value="1" />
                        <asp:ListItem Text="Orange" Value="2" />
                        <asp:ListItem Text="Yellow" Value="3" />
                        <asp:ListItem Text="Green" Value="4" />
                        <asp:ListItem Text="Blue" Value="5" />
                        <asp:ListItem Text="Indigo" Value="6" />
                        <asp:ListItem Text="Violet" Value="7" />
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblColorChoice" runat="server" />
                </td>
            </tr>
        </table>
    </LayoutTemplate>
</uc1:ExpandCollapseRegion>
The code-behind looks like this:
        protected void ddlColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DropDownList ddlColor = ((DropDownList)xpcColor.FindControl("ddlColor")))
            {
                if (!String.IsNullOrEmpty(ddlColor.SelectedValue))
                {
                    ((Label)xpcColor.FindControl("lblColorChoice")).Text = "You chose the color " + ddlColor.SelectedItem.Text;
                }
                else
                {
                    ((Label)xpcColor.FindControl("lblColorChoice")).Text = "";
                }
            }
        }
All that is supposed to happen here is to show in the Label what color was picked, but if no color was picked to then just clear the label. Very simple, no biggie, however....
100% of the time, when I pick a color the SelectedIndexChanged method fires, and the Label control is updated with the text. I can pick one color, the another, then another, and so forth over and over and the thing works great. However, if after choosing a color, I select the blank item of the DDL, the SelectedIndexChanged method *does not* fire, ever.
I wanted to see if this had something to do with the value being selected, so I added a new ListItem before the blank one (making the blank ListItem the second option):
<asp:ListItem Text="White" Value="0" />
Now when I run the page, I can choose a color, the label is updated, choose the blank one and the label is cleared, but if I select "White", the page does a PostBack yet the SelectedIndexChanged method once again *does not* fire.
I have never run into this before and admit I am a bit stumped as to the cause.
The problem may well be in my custom control, but I am hesitant to think so as the DDL functions correctly for all selections, except for the first one. Also the DDL choice as well as the Label text survives a PostBack, so I am not sure this is a ViewState issue either.

I am pretty much stumped here what's going on. If anyone else has seen this, run across this, or may have some input of possible fixes, I am all ears.  Much appreciated.
Posted

1 solution

Hi selected_index changeevent it wont fire for first item, y because it is already selected one,so take any other item in first place like -select-

XML
<asp:DropDownList ID="ddlColor" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlColor_SelectedIndexChanged">
                        <asp:ListItem Selected="True"  Text="-select-" Value="" />
                        <asp:ListItem Text="Red" Value="1" />
                        <asp:ListItem Text="Orange" Value="2" />
                        <asp:ListItem Text="Yellow" Value="3" />
                        <asp:ListItem Text="Green" Value="4" />
                        <asp:ListItem Text="Blue" Value="5" />
                        <asp:ListItem Text="Indigo" Value="6" />
                        <asp:ListItem Text="Violet" Value="7" />
                    </asp:DropDownList>
 
Share this answer
 
Comments
Gordon Beeming 27-Jul-11 15:54pm    
General my adding select as the first item is a good thing so the user knows they need to still provide that input. 5 =D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900