ASP.NET Topics include: System.Web classes, Web Forms and Web Controls.

Go Back  Xtreme .NET Talk > .NET > ASP.NET > Repeater ItemCommand in UpdatePanel causes full postback


Reply
 
Thread Tools Display Modes
  #1  
Old 08-15-2007, 09:03 AM
MrPaul's Avatar
MrPaul MrPaul is offline
Contributor

Preferred language:
C#
 
Join Date: Jun 2006
Location: Hampshire, UK
Posts: 428
MrPaul is on a distinguished road
Exclamation Repeater ItemCommand in UpdatePanel causes full postback



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:

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...
__________________
Never trouble another for what you can do for yourself.
Reply With Quote
  #2  
Old 08-15-2007, 09:06 AM
MrPaul's Avatar
MrPaul MrPaul is offline
Contributor

Preferred language:
C#
 
Join Date: Jun 2006
Location: Hampshire, UK
Posts: 428
MrPaul is on a distinguished road
Exclamation 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>
            <&#37;# 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#:
Code:
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!
__________________
Never trouble another for what you can do for yourself.

Last edited by MrPaul; 08-15-2007 at 10:39 AM.
Reply With Quote
  #3  
Old 08-15-2007, 10:44 AM
MrPaul's Avatar
MrPaul MrPaul is offline
Contributor

Preferred language:
C#
 
Join Date: Jun 2006
Location: Hampshire, UK
Posts: 428
MrPaul is on a distinguished road
Smile 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.
__________________
Never trouble another for what you can do for yourself.
Reply With Quote
  #4  
Old 09-20-2007, 07:40 AM
Frederick Frederick is offline
Newcomer
 
Join Date: Sep 2007
Posts: 1
Frederick is on a distinguished road
Default Re: Repeater ItemCommand in UpdatePanel causes full postback

You are officialy my hero of the day
Reply With Quote
  #5  
Old 08-29-2008, 10:52 PM
jsm11482 jsm11482 is offline
Newcomer
 
Join Date: Aug 2008
Posts: 1
jsm11482 is on a distinguished road
Default Re: Repeater ItemCommand in UpdatePanel causes full postback

Wow, that is embarrassing! I've been trying to get my UpdatePanel working for hours now! Thank you!
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Repeater Control, Check value before calling Repeater.Databind() Nate Bross ASP.NET 4 04-24-2007 04:22 AM
Datagrid ItemCommand event not firing mark007 ASP.NET 2 07-26-2005 07:53 PM
Repeater eramgarden ASP.NET 2 05-06-2004 05:28 PM
itemcommand event wsyeager ASP.NET 1 08-25-2003 11:01 AM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe