简体   繁体   中英

C# .NET DropDownList OnSelectedIndexChanged not firing

Currently I have a dropdownlist in a asp:repeater. The dropdownlist gots two different events.

DataBinding and SelectedIndexChanged. But the SelectedIndexChanged just won't trigger not matter what.

Here's my ASP code:

<asp:UpdatePanel runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Repeater runat="server" ID="_repArticles">
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <img width="130" height="100" src='<%# Eval("ImageFilePath") %>' /> 
                                </td>
                                <td>
                                    <%# Eval("Price") %>
                                </td>
                                <td>
                                    <asp:DropDownList AutoPostBack="true" runat="server" ID="_ddlQuantity" OnDataBinding="_ddlQuantity_DataBinding" OnSelectedIndexChanged="_ddlQuantity_SelectedIndexChanged"></asp:DropDownList>
                                </td>
                                <td>
                                    <%# Eval("TotalPrice") %>
                                </td>
                                <td>
                                    <asp:LinkButton runat="server" ID="_btnRemove" OnClick="_btnRemove_Click" CssClass="close" ToolTip='<%$ Resources: Resource, Remove %>' CommandArgument='<%# Eval("ProductId") %>' ForeColor="Transparent" BackColor="Transparent"></asp:LinkButton>
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </ContentTemplate>
            </asp:UpdatePanel>

And here's my C# code:

protected void _ddlQuantity_DataBinding(object sender, EventArgs e)
    {
        var ddl = sender as DropDownList;

        for (int i = 1; i < Convert.ToInt32(Eval("Stock")); i++)
            ddl.Items.Add(new ListItem(i.ToString(), i.ToString()));

        ddl.SelectedValue = Eval("Quantity").ToString();
    }

    protected void _ddlQuantity_SelectedIndexChanged(object sender, EventArgs e)
    {
        var ddl = sender as DropDownList;

        this.MasterPage.UpdateCartItem(Convert.ToInt32(Eval("ProductId")), Convert.ToInt32(ddl.SelectedValue));

        ddl.SelectedValue = Eval("Quantity").ToString();
    }

I start to think that you can't use both of these events, does anyone know what I am doing wrong?

You can try changing

UpdateMode="Always" 

from

UpdateMode="Conditional"

Well I got the solution, and it's a facepalm moment. I rebinded the repeater on the page load, forgot to set the "if (!IsPostBack)" around it.

Put Triggers in UpdatePanel and then try

<asp:UpdatePanel runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Repeater runat="server" ID="_repArticles">
                    <ItemTemplate>
                        <tr>
                            <td>
                                <img width="130" height="100" src='<%# Eval("ImageFilePath") %>' /> 
                            </td>
                            <td>
                                <%# Eval("Price") %>
                            </td>
                            <td>
                                <asp:DropDownList AutoPostBack="true" runat="server" ID="_ddlQuantity" OnDataBinding="_ddlQuantity_DataBinding" OnSelectedIndexChanged="_ddlQuantity_SelectedIndexChanged"></asp:DropDownList>
                            </td>
                            <td>
                                <%# Eval("TotalPrice") %>
                            </td>
                            <td>
                                <asp:LinkButton runat="server" ID="_btnRemove" OnClick="_btnRemove_Click" CssClass="close" ToolTip='<%$ Resources: Resource, Remove %>' CommandArgument='<%# Eval("ProductId") %>' ForeColor="Transparent" BackColor="Transparent"></asp:LinkButton>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </ContentTemplate>
<Triggers>
            <asp:PostBackTrigger ControlID="_ddlQuantity" />
        </Triggers>
        </asp:UpdatePanel>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM