InvalidOperationException:PostBack触发器无法在DataList的ItemTemplate中找到控件

时间:2009-12-01 14:48:00

标签: asp.net asp.net-ajax postback

我想在点击asp:LinkBut​​ton“addToCartButton”时触发PostBack(参见下面的代码)。

我已经使用asp:UpdatePanel“updPnlProductsList”声明了一个PostBack触发器(参见下面的代码)。

<asp:Panel ID="pnlProductsList" runat="server" Visible="false">
<asp:UpdatePanel ID="updPnlProductsList" runat="server" UpdateMode="Conditional">
<ContentTemplate>
    <div class="featured">
        <h3>Product listing</h3>
        <div class="product clearfix">
            <asp:DataList ID="productsList" runat="server" DataKeyField="prodid" OnItemCommand="productsList_ItemCommand"
                OnItemDataBound="productsList_ItemDataBound">
                <HeaderTemplate>
                    <table>
                        <col width="85" />
                        <col width="315" />
                        <col width="85" />
                        <col width="315" />
                        <col width="85" />
                        <tr>
                            <th align="left">
                                &nbsp;
                            </th>
                            <th align="left">
                                Product Description
                            </th>
                            <th align="center">
                                In Stock
                            </th>
                            <th align="center">
                                Price
                            </th>
                            <th align="left">
                                &nbsp;
                            </th>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td height="85" valign="top">
                            <asp:HyperLink ID="standardImage" Style="float: left; margin-right: 5px; border: 2px;
                                vertical-align: top;" Width="75" Height="75" runat="server"></asp:HyperLink>
                        </td>
                        <td height="85" valign="top">
                            <asp:LinkButton ID="lbProductDescription" CommandName="show_product" runat="server" />
                            <p>
                                <asp:Label ID="MfPartNo" runat="server" /></p>
                        </td>
                        <td height="85" align="center" valign="top">
                            <p>
                                <asp:Label ID="inventoryTextLabel" runat="server" /></p>
                        </td>
                        <td style="padding-bottom: 10px;" height="85" align="center" valign="top">
                            <div class="product-actions clearfix">
                                <p class="price">
                                    <strong>
                                        <asp:Label ID="sellPriceLabel" runat="server" Style="font-size: 0.9em;" /></strong>
                                </p>
                            </div>
                        </td>
                        <td style="padding-bottom: 10px;" height="85" valign="top">
                            <div class="product-actions clearfix">
                                <p class="view">
                                    <asp:LinkButton ID="addToCartButton" runat="server" Text="<span><strong>Buy</strong></span>"
                                        CommandName="add_to_cart" class="newactionbutton" />
                                </p>
                            </div>
                        </td>
                    </tr>
                    </div>
                </ItemTemplate>
                <SeparatorTemplate>
                    <tr>
                        <td colspan="5" style="border-bottom: dotted 1px gray; line-height: 0.1em;">
                            &nbsp;
                        </td>
                    </tr>
                </SeparatorTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:DataList>
        </div>
    </div>
</ContentTemplate>
<Triggers>
    <asp:PostBackTrigger ControlID="addToCartButton" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
<!-- /The all new Products List. -->

不幸的是,当我运行此代码时,我收到错误:

InvalidOperationException:在UpdatePanel'ligupPnlProductsList'中找不到ID为“addToCartButton”的控件。

请有人帮我在DataList的ItemTemplate中引用'addToCartButton'。

或许我可以在asp:LinkBut​​ton代码后面导致PostBack?我在C#编码。

亲切的问候

沃尔特

1 个答案:

答案 0 :(得分:1)

Edit2:建议的第一个解决方案无法正常工作。请按照建议here

尝试此操作(源自您已尝试的内容)
productsList_ItemDataBound(object sender, DataListItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
       ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
       scriptManager.RegisterPostBackControl(e.Item.FindControl("addToCartButton")); 
       // Add this update call.
       updPnlProductsList.Update();
    }
}

在服务器端事件OnRowDataBound()上添加您的控件。此时,您将向触发器列表中添加正确的ID。

Edit1:这就是我的想法。我没有测试它......

        protected void gv_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType = DataControlRowType.DataRow)
            {
                // TODO: Find control within the row.
                Control control = null;

                var trigger = new AsyncPostBackTrigger();
                trigger.ControlID = control.ID;

                updPnlProductsList.Triggers.Add(trigger);

            }
        }