c# - 页面上的 PostBack 后 GridView 上的 jQuery 函数停止工作(鼠标悬停,单击)

标签 c# jquery .net gridview postback

我所有的 jQuery 函数在第一次加载页面时都能完美运行,在我执行回发后,这会导致为 GridView 填充 rowfilter 并根据 rowfilter 再次绑定(bind) Grid。我的 GridView 完全由代码构建并呈现为表格。在此 PostBack 之后,函数 mouseover、mouseout 和单击我表格中的表格行不再起作用。

我将我的 jQuery 函数放在 document.ready 函数中。我还注意到在回发之前这被添加到行的 html 中:

<tr jQuery1320916="3">

所以基本上是每行末尾的一个序列 (3) 和一些前面带有 jQ​​uery 的随机数。回发后,这将从行中删除。

这是我的 jQuery 代码的一部分(第一次点击功能在 PostBack 之后仍然有效,但 GridView 上的功能不再有效):

 $(document).ready(function(){
        //start click function select all
        $('input[id$="SelectDeselectAllBox"]').click(function(){            
        //start if
        if($(this).is(':checked')){
           $(this).parent().parent().parent().parent().children().find('input:checkbox').attr('checked',true).closest('tr').addClass('SelectedRow')
        }else{
            $(this).parent().parent().parent().parent().children().find('input:checkbox').attr('checked',false).closest('tr').removeClass('SelectedRow')};
        //end if

        //end click function select all
             });


        //highligth hovered row
             $('table[id^=taskgrid] tbody tr').mouseover(function () {
                 $(this).addClass('HightlightRow');
             }).mouseout(function () {
                 $(this).removeClass('HightlightRow');
             });            

        //end document ready
        });

我是否遗漏了什么,在此先感谢。

我确实发现 jQuery 不再通过这种方法找到我的 gridview:

$('table[id^=taskgrid] tbody tr'),如果我填写了taskgrid的总ID,那么它确实有效。但这不是我的选择。

    //is everthing checked? start if
 if($(this).parent().find('input:checkbox:not(:checked)').length == 0){
    $(this).parent().parent().parent().parent().find('input:checkbox:first').attr('checked',true)
    }else{
    $(this).parent().parent().parent().parent().find('input:checkbox:first').attr('checked',false)};
//end if

最佳答案

问题是,您使用的是“开头为”选择器:

$('table[id^=taskgrid] tbody tr')

最好为 ASP.NET WebForms 生成的 ID 使用“结束于”选择器。假设您的 GridView id 是“taskgrid”,请尝试使用以下选择器:

$('table[id$="taskgrid"] tbody tr')

如果您的目标是多个表格元素并且“taskgrid”只是 id 的一部分,您可以使用“contains”选择器:

$('table[id*="taskgrid"] tbody tr')

关于c# - 页面上的 PostBack 后 GridView 上的 jQuery 函数停止工作(鼠标悬停,单击),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8077439/

相关文章:

C#协变和继承

c# - .Net Core 3.0 Nginx 不提供静态文件

c# - 将帐户链接到事件 CRM

使用 ASP.NET MVC 4 的 C# Rest Web 服务

javascript - 向 jQuery Sparklines 插件的每个值添加字符串标签

c# - 我可以检测对象是否调用了 GC.SuppressFinalize 吗?

c# - 类型 {1} 中不存在必需的属性 {0}。添加FK时的 Entity Framework (模型优先)

javascript - Bootstrap 轮播和 Django

php - 如何使用 PHP 连接 Dropzone.js 上传的分块文件?

javascript - DropDownList 客户端 onchange 停止服务器端 SelectedIndexChanged 事件