C#.NET DropDownList OnSelectedIndexChanged未触发

时间:2014-06-09 15:54:09

标签: c# .net events

目前我在asp:repeater中有一个下拉列表。下拉列表包含两个不同的事件。

DataBinding和SelectedIndexChanged。但是SelectedIndexChanged不会触发什么不重要。

这是我的ASP代码:

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

这是我的C#代码:

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();
    }

我开始认为你不能同时使用这两个事件,有谁知道我做错了什么?

3 个答案:

答案 0 :(得分:0)

您可以尝试更改

UpdateMode="Always" 

来自

UpdateMode="Conditional"

答案 1 :(得分:0)

嗯,我得到了解决方案,这是一个很好的时刻。我在页面加载时重新启动了转发器,忘了设置&#34; if(!IsPostBack)&#34;在它周围。

答案 2 :(得分:0)

将触发器放入UpdatePanel,然后尝试

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