Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

[attach]5598._xfImport[/attach]

 

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.
Posted (edited)

Continued...

 

Code for previous post.

 

XHTML:

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

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:

Edited by MrPaul
Never trouble another for what you can do for yourself.
Posted

It's all in the ID

 

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

 

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

Never trouble another for what you can do for yourself.
  • 1 month later...
  • 11 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...