Updatepanel gives full postback instead of asyncpostback

人走茶凉 提交于 2019-12-19 06:07:51

问题


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


回答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.




回答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" 


来源:https://stackoverflow.com/questions/4319156/updatepanel-gives-full-postback-instead-of-asyncpostback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!