Updatepanel gives full postback instead of asyncpostback
Solution 1
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.
Solution 2
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"
Lars Holdgaard
An entrepreneur since I was 18. Been building a lot of stuff: some successful, some not so successful Currently building the best debt collection company in the world at Likvido. In my free time (when not tinkering with code), I'm very much into crypto, traveling and being digital-nomad when it suits my lifestyle... And I blog at LarsHoldgaard.com!
Updated on June 23, 2022Comments
-
Lars Holdgaard almost 2 years
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 over 13 yearsUrh.. Not what I had hoped.. But that's how it's done! Thanks a lot! I'm really grateful! :-)
-
MikeTeeVee almost 13 yearsThis 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.
-
Joe Stellato almost 10 yearsEpic Answer, so Easy. Thank you!
-
GPMorgan over 9 yearsThank you very much for this, kicking myself! UpdatePanel1.Update();
-
Josh M. about 9 yearsI hate that this is the solution, but it works! Thanks!
-
Chris about 7 yearsI already had AutoID on globally, but I simply needed to add IDs to my linkbuttons in my listview. No triggers were needed.
-
Dubs over 4 yearsEven with ClientID set to Static globally, setting my RadioButtonLists and CheckBoxLists to AutoID was still the solution.