[英]Child control UpdatePanel causing full postback
I'm trying to extract an UpdatePanel out to a child UserControl, so that I can use it multiple times in the same page. 我试图将UpdatePanel提取到子UserControl,以便可以在同一页面中多次使用它。 However, when I call __doPostback on the child control from the main page (at the end of page load - a couple of other things need to be obtained before data can be loaded), it refreshes the entire page, causing an infinite loop. 但是,当我从主页面在子控件上调用__doPostback时(在页面加载结束时–需要其他一些操作才能加载数据),它将刷新整个页面,从而导致无限循环。
What really drives me up the wall, though, is that when the exact same UpdatePanel is embedded into the main page instead of being in a child control, partial postback works! 但是,真正使我烦恼的是,当完全相同的UpdatePanel嵌入到主页而不是放在子控件中时,部分回发就可以了! I'm hoping someone has an idea why. 我希望有人知道为什么。
MainPage.ascx: MainPage.ascx:
<%@ Register TagPrefix="test" TagName="GridControl" Src="~/Controls/GridControl.ascx" %>
<asp:ScriptManager ID="SM" runat="server" EnablePageMethods="true">
<test:GridViewControl ID="child" runat="server" />
<asp:UpdatePanel ID="embedded" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView runat="server" AllowPaging="true" OnPageIndexChanging="gridView_PageIndexChanging">
<Columns>
...
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
MainPage.js: MainPage.js:
pageLoad = function ()
{
// Works great: no infinite loop, GridView paging works, embedded.Visible=false hides the grid
__doPostBack("<%= embedded.ClientID %>");
// All of the problems
$("#grid").update();
}
pageLoad();
ChildControl.ascx: ChildControl.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GridControl.ascx.cs" Inherits="Control" %>
<script type="text/javascript">
var upClientID = "<%= updatePanel.ClientID %>";
// Trigger the post-back so that the update panel re-loads data.
$.fn.update = function ()
{
__doPostBack(upClientID);
};
</script>
<!-- Exactly the same as the embedded UpdatePanel -->
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gridView"runat="server" AllowPaging="true" OnPageIndexChanging="gridView_PageIndexChanging">
<Columns>
...
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
I'm actually running into quite a number of problems with this child control - setting the Visible property doesn't do anything, GridView paging doesn't work... it surprises me that creating a simple child control should be so difficult. 我实际上对该子控件遇到了很多问题-设置Visible属性没有任何作用,GridView分页不起作用...让我惊讶的是,创建一个简单的子控件应该如此困难。
(Answering my own question) (回答我自己的问题)
Not a fix for the issue exactly, but I eventually embedded the UpdatePanel into the main page and extracted the GridView as a child control. 不能完全解决此问题,但最终我将UpdatePanel嵌入到主页中,并将GridView提取为子控件。 There was still some duplicate code, but postback began working out okay. 仍然有一些重复的代码,但是回发开始可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.