20

In my page, I have an LinkButton inside repeater, but the UpdatePanel cannot find the LinkButton to AsyncPostBackTrigger.

Here is mycode.aspx

<asp:ScriptManager ID="Test1" runat="server" />
<asp:UpdatePanel ID="TestUpdate" runat="server" UpdateMode="Always">
<ContentTemplate>
<table width="100%">
<tr valign="top">
    <td width="50%">
        <asp:Repeater ID="productList" runat="server" onitemcommand="productList_ItemCommand">
        <HeaderTemplate>
        <ul type="disc">
        </HeaderTemplate>
        <ItemTemplate>
        <li>
            <asp:Label id="L1" runat="server" Text='<%# Eval("productName") %>'></asp:Label><br />
            Price:
            <asp:Label runat="server" Text='<%# Eval("productPrice") %>' ></asp:Label>&nbsp;Bath<br />
            <img alt="" src="Images/product/product<%# Eval("productID") %>.png" style="width: 200px; height: 130px" /><br />
            <asp:TextBox ID="num_product" runat="server" Text="0"></asp:TextBox><br />
            <asp:LinkButton ID="order_button" runat="server"><img alt="" src="~/Images/button/order.png" /></asp:LinkButton>
        </li>
        </ItemTemplate>
        <FooterTemplate>
        </ul>
        </FooterTemplate>
        </asp:Repeater> 
    <td>
    <span class="labelText">Order list</span>
        <asp:BulletedList ID="orderList" runat="server" BulletStyle="Numbered">
        </asp:BulletedList> 
    </td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>

Here is mycode.aspx.cs

protected void productList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //button
        /*LinkButton btn = new LinkButton();
        btn.ID = "order_button";
        btn.Click += LinkButton1_Click;
        Test1.RegisterAsyncPostBackControl(btn);*/

        LinkButton btn = (LinkButton)e.Item.FindControl("order_button");
        btn.Click += LinkButton1_Click;
        Test1.RegisterAsyncPostBackControl(btn);

            /*AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
            trigger.ControlID = btn.ClientID;
            trigger.EventName = "Click";
            TestUpdate.Triggers.Add(trigger);*/

    }
   protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //string name = ProductName1.Text.ToString();
        //int price = System.Convert.ToInt32(ProductPrice1.ToString(), 10);
        //int number = System.Convert.ToInt32(TextBox1.ToString(),10);
        //orderList.Items.Clear();
        //orderList.Items.Add(new ListItem(name));
        //ListItem product1 = new ListItem();
        //product1.Text = name;
        orderList.Items.Add("test");
    }

I tried many methods, but the page is still refresh. Do you have any suggestion?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1128331
  • 707
  • 4
  • 10
  • 20
  • 2
    Altough the answer you marked as correct is indeed correct, the correct answer is from Matt F. Due to the linkbutton not being generated with the proper ID, you are required to register it manually. If it had the correct ID generated by AutoID, the scripts will detect the control and fire the Ajax request. Also, its much cleaner to change a property then having to write special code.... – Nuno Agapito Mar 12 '18 at 16:46

3 Answers3

51

Inside ItemCreated event of the Repeater control register the button with ScriptManager.

//Inside ItemCreatedEvent
ScriptManager scriptMan = ScriptManager.GetCurrent(this);
LinkButton btn = e.Item.FindControl("order_button") as LinkButton;
if(btn != null)
{
    btn.Click += LinkButton1_Click;
    scriptMan.RegisterAsyncPostBackControl(btn);
}
Eugene S.
  • 3,256
  • 1
  • 25
  • 36
  • Thanks Eugene. Saved lot of time for me. Cheers – Nuthan Gowda Jun 11 '14 at 08:02
  • I needed to do the opposite of this - make a link button in a repeater do a full postback instead of a async postback from within the updatepanel. This worked perfectly for that. I just used `scripMan.RegisterPostBackControl(btn)` instead. – ahwm Jan 12 '17 at 19:02
  • how does this work?, where are u saying wich to wich updatepanel does that triggers belong – Angel Q May 31 '19 at 13:23
  • Registering the button with ScriptManager handles that. https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.scriptmanager.registerasyncpostbackcontrol?view=netframework-4.8 – Eugene S. May 31 '19 at 18:15
18

I had similar problem, but I didn't want to update the whole repeater, only a content outside of the repeater... so what I did was

1. Add the repeater

<asp:Repeater ID="productList" runat="server">
  <!-- my repeater -->
<asp:Repeater>

2. Add the Update Panel with the updatable content, and the trigger

<asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
        <!-- when click on repeater's links, this content will be updated -->
    </ContentTemplate>
    <Triggers>
        <!-- trigger will be the repeater's links/btn that generate postback -->
        <asp:AsyncPostBackTrigger ControlID="productList" />
    </Triggers>
</asp:UpdatePanel>
Jaider
  • 14,268
  • 5
  • 75
  • 82
16

Adding the following attribute to the page directive containing the repeater and linkbutton will also work:

<%@ page ClientIDMode="AutoID" %>

I had a control that needed to work both asynchronously and full postback, so using the ScriptManager.RegisterAsyncPostBackControl would not work for me. By enclosing the control (which contained a repeater and linkbutton) inside of an UpdatePanel, the linkbutton would cause an asynchronous postback. With no updatepanel, the linkbutton would cause a fullpostback.

Hope this helps someone else.

Matt F
  • 231
  • 2
  • 10