子控件UpdatePanel导致完全回发

时间:2015-12-29 20:30:19

标签: javascript asp.net

我正在尝试将UpdatePanel提取到子UserControl,以便我可以在同一页面中多次使用它。但是,当我从主页面调用子控件上的__doPostback时(在页面加载结束时 - 在加载数据之前需要获取其他一些东西),它会刷新整个页面,从而导致无限循环。 / p> 但是,真正让我感到震惊的是,当完全相同的UpdatePanel嵌入主页而不是在子控件中时,部分回发会起作用!我希望有人知道为什么。

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:

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:

<%@ 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>

我实际上遇到了很多关于这个子控件的问题 - 设置Visible属性没有做任何事情,GridView分页不起作用......让我感到惊讶的是创建一个简单的子控件应该是这样的困难的。

1 个答案:

答案 0 :(得分:0)

(回答我自己的问题)

不完全解决问题,但我最终将UpdatePanel嵌入到主页面中,并将GridView作为子控件提取出来。仍然有一些重复的代码,但回发开始工作正常。

相关问题