本文介绍了为什么DDL SelectedIndexChanged()不会在第一个ListItem上触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

<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>


这篇关于为什么DDL SelectedIndexChanged()不会在第一个ListItem上触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 18:21