在GridView的分页回调之后执行JavaScript

时间:2022-08-23 19:34:54

I have a GridView with EnableSortingAndPagingCallbacks enabled. When the user clicks to change pages, a callback is performed and the GridView is updated. I need to run a JavaScript function immediately after this happens so I can perform some client-side actions on the new page of data. How can I accomplish this?

我有一个启用了EnableSortingAndPagingCallbacks的GridView。当用户单击以更改页面时,将执行回调并更新GridView。发生这种情况后,我需要立即运行一个JavaScript函数,以便在新的数据页面上执行一些客户端操作。我怎样才能做到这一点呢?

The closest I've found to my question is this: How to have a javascript callback executed after an update panel postback?. However, using the pageLoad() function won't work here because pageLoad() doesn't seem to be triggered after a GridView callback.

我发现最接近我的问题是:如何在更新面板回发后执行javascript回调?但是,使用pageLoad()函数在这里行不通,因为在GridView回调之后,pageLoad()似乎不会被触发。

I need to have this work with IE7, or otherwise I'd use the DOMSubtreeModified event listener.

我需要在IE7上进行这项工作,否则我将使用DOMSubtreeModified事件侦听器。

Sample code where GridView1_PageIndexChanging and pageLoad won't fire.

GridView1_PageIndexChanging和pageLoad不会触发的示例代码。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test_script.aspx.cs" Inherits="test_script" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<form id="form1" runat="server">
    <asp:ToolkitScriptManager ID="ScriptManager1" runat="server" ScriptMode="Release" />
    <asp:GridView ID="GridView1" runat="server" AllowPaging="true" 
        EnableSortingAndPagingCallbacks="true" DataSourceID="SqlDataSource1" 
        OnPageIndexChanging="GridView1_PageIndexChanging" />
</form>
<script type="text/javascript">
    function pageLoad(sender, args) {
        alert('pageLoad');
    }
</script>

Code behind:

背后的代码:

public partial class test_script : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // This doesn't fire when EnableSortingAndPagingCallbacks is set to true
    }
}

In the above code, pageLoad() fires when the page is first loaded but it does not fire after the GridView is paged. In the code behind, Page_Load fires when the GridView is paged but GridView1_PageIndexChanging() does not.

在上面的代码中,pageLoad()在首次加载页面时触发,但在页面被分页之后不会触发。在后面的代码中,页面_load在GridView被分页时触发,而GridView1_PageIndexChanging()则没有。

If I change EnableSortingAndPagingCallbacks to false, all functions fire as you would expect on each GridView page change.

如果我将EnableSortingAndPagingCallbacks更改为false,那么所有函数都会按您在每个GridView页面更改时所期望的那样触发。

2 个解决方案

#1


3  

This should work:

这应该工作:

   Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        Me.GridView1.PageIndex = e.NewPageIndex
        BindGridview() 'This is what binds the control
        If Not ClientScript.IsClientScriptBlockRegistered(Me.GetType(), "myScript") Then
            ScriptManager.RegisterClientScriptBlock(Me.GridView1, Me.GetType(), "myScript", "alert('Done with paging');", True)
        End If
    End Sub

Since you did not specify the language I used the first one that opened up with my visual studio.

由于您没有指定语言,所以我使用了与visual studio一起开放的第一个语言。

#2


0  

If you want to run any client after a gridview function you can use the <ClientSettings></ClientSettings> for you case i ClientEvents-OnDataBinding="YoueFunctionFromScript()"

如果您想在gridview函数之后运行任何客户端,您可以使用 for you case i ClientEvents-OnDataBinding="YoueFunctionFromScript()"

#1


3  

This should work:

这应该工作:

   Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        Me.GridView1.PageIndex = e.NewPageIndex
        BindGridview() 'This is what binds the control
        If Not ClientScript.IsClientScriptBlockRegistered(Me.GetType(), "myScript") Then
            ScriptManager.RegisterClientScriptBlock(Me.GridView1, Me.GetType(), "myScript", "alert('Done with paging');", True)
        End If
    End Sub

Since you did not specify the language I used the first one that opened up with my visual studio.

由于您没有指定语言,所以我使用了与visual studio一起开放的第一个语言。

#2


0  

If you want to run any client after a gridview function you can use the <ClientSettings></ClientSettings> for you case i ClientEvents-OnDataBinding="YoueFunctionFromScript()"

如果您想在gridview函数之后运行任何客户端,您可以使用 for you case i ClientEvents-OnDataBinding="YoueFunctionFromScript()"

相关文章