3

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?

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.

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

Sample code where GridView1_PageIndexChanging and pageLoad won't fire.

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

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

Community
  • 1
  • 1
user2250630
  • 33
  • 1
  • 6

2 Answers2

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.

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
  • This works fine during normal GridView postbacks but I haven't been able to get RegisterClientScriptBlock working when using EnableSortingAndPagingCallbacks. Also PageIndexChanging doesn't even fire during a paging callback. – user2250630 Apr 05 '13 at 23:53
  • Then my dear friend you must have something very wrong in your code (maybe some javascript error) because both the pageLoad and this works for me. – Hanlet Escaño Apr 06 '13 at 01:58
  • I added some example code showing where I can't get PageIndexChanging or pageLoad to fire. Could you see where my code differs from yours? – user2250630 Apr 08 '13 at 02:39
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()"

TomerMahari
  • 409
  • 3
  • 8