Updatepanel 提供完整回发而不是异步回发
我遇到了一个似乎非常著名的问题:我的 updatepanel 触发完整回发而不是异步回发。正常的解决方案是为所有动态添加的控件提供一个 ID,我已经这样做了,但我仍然得到完整的回发而不是异步回发...
代码如下:
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>
有趣的部分是后面的 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);
所以,我实际上向菜单添加了一个 AsyncPostBackTrigger,因此应该注册 ItemCommand 事件。当我单击此上下文菜单中的某个项目时,会发生完整的回发。
我一直在尝试在没有帮助的情况下使用 ChildrenAsTriggers 属性。我还上下移动了 AsyncPostBackTrigger 代码,同样没有帮助。
预先非常感谢..! 拉尔斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 UpdatePanel 的面板内的 ListView 内填充 CheckBoxList 时,我也有同样的经历。通过在 CheckBoxList 中添加以下代码解决了这个问题:
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:
来自 AsyncPostBackTrigger 文档:
From the AsyncPostBackTrigger documentation: