ASP.net: 刷新GridView而不刷新整个页面? (AsyncPostBackTrigger 真的很慢)

ASP.net: Refresh GridView without refreshing the whole page? (AsyncPostBackTrigger really slow)

我是 ASP.net 的新手,正在尝试使一些超慢的代码 运行 更快。

目前,代码在 UpdatePanel 中使用 GridView。 UpdatePanel 位于模态弹出窗口 window 中。无论何时打开该模式,都必须刷新内容。我们通过使用 AsyncPostBackTrigger 来做到这一点,据我所知,它在 returning 和呈现 table.

之前经历了整个页面生成周期

.aspx.cs

public void UpdateWatchListPopup(object sender, System.EventArgs e)
{
    grdWatchList.DataBind();
}

.aspx:

<asp:UpdatePanel ID="UpdatePanel3" runat="server" >

    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="UpdateWatchListPopupBtn" EventName="Click" /> 
    </Triggers>

    <ContentTemplate>

        <div style="display:none">
            <asp:Button ID="UpdateWatchListPopupBtn" runat="server" Text="" OnClick="UpdateWatchListPopup" />
        </div>

        <asp:GridView ID="grdWatchList" OnSorting="grdWatchList_Sorting" runat="server" OnRowCreated="grdWatchList_RowCreated" OnRowDataBound="grdWatchList_RowDataBound" AllowSorting="true" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField>

这个真的很慢(需要5秒才能显示结果),还不是因为要return的数据很多!我的猜测是 Page_Load() 正在做一堆不需要刷新特定 GridView 的计算。

有没有其他方法可以异步刷新GridView?我考虑过使用 WebMethod 获取数据,然后从客户端手动重新填充 table 。我想知道是否还有其他选择?

谢谢

您不一定需要 PostBack 来打开弹出窗口。这是一个使用 jQuery UI Dialog.

的片段
<div id="hiddenGrid" style="display: none">
    <asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>

<input type="button" value="Open Dialog" onclick="createPopup()" />

<script type="text/javascript">
    function createPopup() {
        var html = document.getElementById('hiddenGrid').innerHTML;
        $('<div />').html(html).dialog({
            resizable: false,
            draggable: true,
            modal: true,
            width: 600,
            height: 800,
            create: function (event, ui) {
                $(".ui-dialog-titlebar").html("<div onclick=\"closePopup()\" class=\"dialog-closeButton\"></div>");
            },
            open: function (event, ui) {
                $('.ui-widget-overlay').bind('click', function () {
                    closePopup();
                })
            }
        });
    }

    function closePopup() {
        $(".ui-dialog-content").dialog("destroy");
    }
</script>

但是如果你必须打开带有 PostBack 的 Modal,你可以检查它是否是一个 Async PostBack 并跳过你不需要的 Page_Load 中的项目。

protected void Page_Load(object sender, EventArgs e)
{
    if (ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
    {
        //updatepanel page load
    }
    else
    {
        //normal page load
    }
}

您可能需要考虑使用客户端网格,我使用 Jquery 数据表已经有一段时间了,主要是使用 ASPNET MVC,但您也可以将其用于 Web 表单。这真的很容易上手,那里有很多很好的例子,最好的部分是没有回发。此工具内置分页、排序、搜索等功能。

这是网络表单 Jquery 数据表 运行 的一个很好的例子 http://datatables.extrared.net/Sample/

您可以从这里开始使用 Jquery 数据表 https://datatables.net/