Updatepanel gives full postback instead of asyncpostback

Lars Holdgaard picture Lars Holdgaard · Nov 30, 2010 · Viewed 13.9k times · Source

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

Answer

arjan picture arjan · Dec 19, 2010

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"