开发者

InvalidOperationException: PostBack Trigger Cannot Find a Control within the ItemTemplate of a DataList

I would like to trigger a PostBack when the asp:LinkButton "addToCartButton" is clicked (see code below).

I have declared a PostBack Trigger with the asp:UpdatePanel "updPnlProductsList" (see code below).

<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开发者_C百科 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. -->

Unfortunately, when I run this code I get the error:

InvalidOperationException: A control with ID 'addToCartButton' could not be found for the trigger in UpdatePanel 'updPnlProductsList'.

Would someone please help me reference the 'addToCartButton' within the ItemTemplate of the DataList.

Or maybe I can cause a PostBack in the asp:LinkButton code behind? I'm coding in C#.

Kind Regards

Walter


Edit2: First solution suggested cannot work. Try this (derived from what you already tried) as suggested 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();
    }
}

Add your control on the server side event OnRowDataBound(). At this point you will be adding the right id to the Triggers list.

Edit1: This is what I had in mind. I did not test it...

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

            }
        }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜