Updatepanel提供完整的回发而不是asyncpostback

时间:2022-08-25 17:22:27

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

我遇到了一个非常着名的问题:我的updatepanel触发了一个完整的回发而不是异步回发。正常的解决方案是给你动态添加的所有控件一个ID,我已经完成了,但我仍然得到一个完整的回发而不是我的异步回发...

Here's the code:

这是代码:

HTML:

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:

有趣的部分是后面的C#代码(方法PlayerItems_ItemDataBound),如下所示:

            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.

所以,我实际上在菜单中添加了AsyncPostBackTrigger,因此应该注册ItemCommand事件。单击此上下文菜单中的项目时发生的事情是完整的回发事件。

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.

我一直试图在没有帮助的情况下玩ChildrenAsTriggers。我也一直在上下移动AsyncPostBackTrigger代码,也没有帮助。

Thanks a lot beforehand..! Lars

非常感谢事先..!拉尔斯

2 个解决方案

#1


8  

From the AsyncPostBackTrigger documentation:

从AsyncPostBackTrigger文档:

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.

不支持以编程方式添加AsyncPostBackTrigger控件。若要以编程方式注册回发控件,请使用ScriptManager控件的RegisterAsyncPostBackControl方法。然后在控件回发时调用UpdatePanel控件的Update方法。

#2


29  

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:

在UpdatePanel中的Panel内部填充ListView内的CheckBoxList时,我有相同的经验。它通过在CheckBoxList中添加此代码来解决:

ClientIDMode="AutoID" 

#1


8  

From the AsyncPostBackTrigger documentation:

从AsyncPostBackTrigger文档:

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.

不支持以编程方式添加AsyncPostBackTrigger控件。若要以编程方式注册回发控件,请使用ScriptManager控件的RegisterAsyncPostBackControl方法。然后在控件回发时调用UpdatePanel控件的Update方法。

#2


29  

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:

在UpdatePanel中的Panel内部填充ListView内的CheckBoxList时,我有相同的经验。它通过在CheckBoxList中添加此代码来解决:

ClientIDMode="AutoID"