Repeater ItemCommand in UpdatePanel causes full postback

MrPaul

Contributor
Joined
Jun 28, 2006
Messages
428
Location
Hampshire, UK
Usually if I'm having a problem I can find others who've had the same problem, and so discover either a solution or that there is none. After a couple of hours on Google I haven't found a single person reporting this problem, which amazes me considering the simplicity of the task I am trying to perform.

I have an UpdatePanel which contains a Repeater and a two LinkButton controls - one LinkButton causes an item to be added to the Repeater's data list, the other causes a random item to be removed from the list. Both these buttons work as they should, causing a partial page update which updates the UpdatePanel but does not refresh the entire page.

Within the ItemTemplate of the Repeater there is a single LinkButton which when clicked, removes only that item. It's a common enough scenario:

View attachment repeater.png

The problem is that the LinkButtons in the Repeater are not causing a partial page update (or asynchronous postback), they are causing full postbacks! I have tried everything I could think of, including:

  1. Setting ChildrenAsTriggers="false" and manually adding an AsyncPostBackTrigger for the Repeater - no change
  2. Calling ScriptManager.RegisterAsyncPostBackControl in Page_Load - no change
  3. Moving the 2 working LinkButtons outside the UpdatePanel, leaving only the Repeater - no change
  4. Placing a LinkButton inside the FooterTemplate - this also causes a full postback

The fact that both of the LinkButtons outside the Repeater are working correctly indicates that the problem is not due to data binding. Code follows in the next post...
 
Continued...

Code for previous post.

XHTML:
Code:
    <div style="border: solid 5px green;width: 300px">
    <asp:ScriptManager runat="server" ID="scriptMan" />
    
    <asp:UpdatePanel runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
    <ContentTemplate>
        
        <asp:Repeater runat="server" ID="testRep" OnItemCommand="testRep_OnItemCommand">
        <ItemTemplate>
            <%# Container.DataItem.ToString() %> ::
            <asp:LinkButton runat="server">Remove <%# Container.DataItem.ToString() %></asp:LinkButton><br />
        </ItemTemplate>
        </asp:Repeater>
        
        <hr />
        
        <asp:LinkButton runat="server" ID="testAdd" OnClick="testAdd_OnClick">Add New Item</asp:LinkButton><br />
        <asp:LinkButton runat="server" ID="testRemove" OnClick="testRemove_OnClick">Remove Random Item</asp:LinkButton><br />
            
    </ContentTemplate>
    </asp:UpdatePanel>
    </div>

C#:
C#:
public partial class ScratchPlain : System.Web.UI.Page
{
    //Make some data
    static List<string> itms = new List<string>();
    static int          i;  

    static ScratchPlain()
    {
        itms.Add("Item 1");
        itms.Add("Item 2");
        itms.Add("Item 3");
        i = 4;
    }

    //Databind Repeater
    protected override void OnLoad(EventArgs e)
    {
        testRep.DataSource = itms;
        testRep.DataBind();
    }
    //Remove clicked item - CAUSES FULL POSTBACK!
    protected void testRep_OnItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        itms.RemoveAt(e.Item.ItemIndex);
        testRep.DataBind();
    }
    //Add new item
    protected void testAdd_OnClick(object sender, EventArgs e)
    {
        itms.Add("Item " + (i++));
        testRep.DataBind();
    }
    //Remove random item
    protected void testRemove_OnClick(object sender, EventArgs e)
    {
        Random r = new Random();
        itms.RemoveAt(r.Next(itms.Count));
        testRep.DataBind();
    }
}

Thanks in advance, people! :cool:
 
Last edited:
It's all in the ID

The solution to this is embarassingly simple - give the control an ID.

Code:
<asp:LinkButton runat="server" ID="anything">

Works fine.

Slightly annoying since I don't like to give controls an ID unless I refer to them in code, but never mind. :rolleyes:
 
Back
Top