开发者

DDL SelectedIndexChanged() does not fire on first ListItem (ASP.Net C#)

开发者 https://www.devze.com 2023-03-22 16:30 出处:网络
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 Templat

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" V开发者_运维百科alue="7" />
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Label ID="lblColorChoice" runat="server" />
            </td>
        </tr>
    </table>

</LayoutTemplate>

The code-behind looks like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ((Label)xpcColor.FindControl("lblColorChoice")).Text = "";
        }
    }

    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'm still Googl'ing this, but 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.

-- Andrew

0

精彩评论

暂无评论...
验证码 换一张
取 消