简体   繁体   English

您如何从Javascript调用OnPageIndexChanging?

[英]How do you call OnPageIndexChanging from Javascript?

I have code that when a user wants to page through a GridView, it asks them (using a JavaScript confirm) if they want to save the data from the grid. 我有一段代码,当用户想翻页GridView时,它询问他们(使用JavaScript确认)是否要保存网格中的数据。 So, I'm able to get the confirm to work (with the code-behind saving function), but I'm noticing that it's not firing the OnPageIndexChanging method - which basically defeats the purpose here. 因此,我能够(通过代码隐藏保存功能)使确认工作,但是我注意到它没有触发OnPageIndexChanging方法-这基本上违背了这里的目的。

So, to summarize, can JavaScript access the OnPageIndexChanging method? 因此,总而言之,JavaScript可以访问OnPageIndexChanging方法吗?

Thanks a lot 非常感谢

The OnPageIndexChanging event is a server-side event, so it's not surprising that your Javascript handler isn't getting triggered. OnPageIndexChanging事件是服务器端事件,因此没有触发Javascript处理程序也就不足为奇了。


I'm not sure if ASP.Net has a "built in" way to do this; 我不确定ASP.Net是否具有“内置”方式来执行此操作。 but you can do it by attaching your own Javascript listeners. 但是您可以通过附加自己的Javascript侦听器来实现。 Here is the general approach (I'm using JQuery to make it easier): 这是一般方法(我正在使用JQuery使其变得更容易):

  1. Write a JQuery selector that gets all the paging links that you want to confirm. 编写一个JQuery选择器,该选择器将获取您要确认的所有分页链接。
  2. Add a click listener for each of those links 为每个链接添加一个click监听器
  3. Make the confirm function the handler for those listeners 使确认函数成为那些侦听器的处理程序

So, the code would look something like this: 因此,代码如下所示:

$("#grid a").each(function () {
    $(this).click(function () {
        return confirm("really?");
    });
});

Notes 笔记

  1. Here grid is the ID of the GridView control, so #grid a selects every a tag within my grid. 这里grid是的ID GridView控件,所以#grid a每选择a标签我的网格内。
  2. Using return confirm() returns false if the user does not confirm, which effectively cancels the click event. 如果用户未确认,则使用return confirm()返回false ,这将有效地取消click事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM