8

I have run into what seems to be a very famous problem: My updatepanel fires a full postback instead of a async postback. The normal solution is to give all controls you add dynamically an ID, which I have done, but I still get a full postback instead of my async postback...

Here's the code:

HTML:

<asp:UpdatePanel ID="ItemsUpdatePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
   <Triggers>
   </Triggers>    
   <ContentTemplate>
   <asp:ListView ID="PlayerItems" runat="server" GroupItemCount="5" 
                                    onitemdatabound="PlayerItems_ItemDataBound">
   <LayoutTemplate>

   ... Listview stuff ...

    </asp:ListView> 

    </ContentTemplate>
</asp:UpdatePanel>

The interesting part is the C# code behind (method PlayerItems_ItemDataBound), which is like the following:

            ImageButton imgBtn = new ImageButton();
            imgBtn.ID = "itemBtn";
            imgBtn.Width = Unit.Pixel(30);
            imgBtn.ImageUrl = "~/Images/Game/Items/" + myItem.ItemImageUrl;

            ContextMenu menu = new ContextMenu();
            menu.BoundControls.Add(imgBtn);
            menu.ItemCommand += new CommandEventHandler(menu_ItemCommand);

            menu.AutoHide = true;
            menu.RolloverColor = Color.Gray;
            menu.ID = "MenuMenu";

            Panel panel = (Panel)(e.Item.FindControl("ItemPanel"));
            panel.Controls.Add(imgBtn);
            panel.Controls.Add(menu);

            AsyncPostBackTrigger trig = new AsyncPostBackTrigger();
            trig.ControlID = menu.UniqueID;
            trig.EventName = "ItemCommand";
            ItemsUpdatePanel.Triggers.Add(trig);

So, I actually add an AsyncPostBackTrigger to the menu, so the ItemCommand event should be registered. What happends when I click an item in this contextmenu, is a full postback happends.

I have been trying to play with the ChildrenAsTriggers property without help. I have also been moving the AsyncPostBackTrigger code up and down, also without help.

Thanks a lot beforehand..! Lars

Lars Holdgaard
  • 9,496
  • 26
  • 102
  • 182

2 Answers2

31

I had the same experience when populating a CheckBoxList inside a ListView inside a Panel in an UpdatePanel. It was solved by adding this code in the CheckBoxList:

ClientIDMode="AutoID" 
arjan
  • 329
  • 2
  • 3
  • 5
    This works! Thank you so much. MS changed how ClientID's are generated in .net 4.0 from "AutoID" to "Predictable" and I guess the ScriptManager or UpdatePanel's weren't updated correctly to use it. I can't find documentation on why that is anywhere or if it was left that way by design. – MikeTeeVee May 24 '11 at 19:19
  • 3
    Epic Answer, so Easy. Thank you! – Joe Stellato Aug 13 '14 at 17:43
  • 2
    I hate that this is the solution, but it works! Thanks! – Josh M. Mar 25 '15 at 18:29
  • 1
    I already had AutoID on globally, but I simply needed to add IDs to my linkbuttons in my listview. No triggers were needed. – Christopher Mar 29 '17 at 00:32
  • Even with ClientID set to Static globally, setting my RadioButtonLists and CheckBoxLists to AutoID was still the solution. – Dubs Jan 15 '20 at 18:21
8

From the AsyncPostBackTrigger documentation:

Programmatically adding AsyncPostBackTrigger controls is not supported. To programmatically register a postback control, use the RegisterAsyncPostBackControl method of the ScriptManager control. Then call the Update method of the UpdatePanel control when the control posts back.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479