views:

624

answers:

1

Hi Group,

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

A: 

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

            }
        }
joerage
Thanks joerage for your quick response. Would you please illustrate how I can do that?
Walter Lockhart
Hi joerage. I tried adding this to my code behind: protected void productsList_ItemDataBound(object sender, DataListItemEventArgs e) { // Handles the onItemDataBound event of the productsList. if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page); scriptManager.RegisterPostBackControl(e.Item.FindControl("addToCartButton"));}It didn't work. Same error.
Walter Lockhart
Hi joerage. I tried this:protected void productsList_ItemDataBound(object sender, DataListItemEventArgs e) { // Handles the onItemDataBound event of the productsList. if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Control control = null; control = e.Item.FindControl("addToCartButton"); PostBackTrigger trigger = new PostBackTrigger(); trigger.ControlID = control.ID; updPnlProductsList.Triggers.Add(trigger); }}but it failed with the same error.
Walter Lockhart
Thanks joerage.
Walter Lockhart
Ok, not sure... Try with control.ClientID instead of control.ID
joerage
Thanks joerage. Tried ClientID and got a similar error: A control with ID 'ctl00_cPH_ctrlProductsList_productsList_ctl01_addToCartButton' could not be found for the trigger in UpdatePanel 'updPnlProductsList'.
Walter Lockhart
Ok, just found out from this post: forums.asp.net/t/1088040.aspx (last comment) that Programmatically adding AsyncPostBackTrigger controls is not supported. Use the RegisterAsyncPostBackControl(Control) method of the ScriptManager control to programmatically register a postback control, and then call the Update() method of the UpdatePanel when the control posts back.' You already tried that, but try adding the Update call to your updatepanel.
joerage