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

时间:2011-11-10 09:33:43

标签: c# jquery .net gridview postback

我的所有jQuery函数在第一次加载页面时都能正常工作,之后我执行回发操作会导致为GridView填充rowfilter并根据rowfilter再次绑定Grid。我的GridView完全由代码构建并呈现为表格。在这个PostBack之后,函数mouseover,mouseout和点击表格中的表行不再有效了。

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

<tr jQuery1320916="3">

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

这是我的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

1 个答案:

答案 0 :(得分:1)

问题是,您正在使用“以...开头”选择器:

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

最好为ASP.NET WebForms生成的id使用“ends with”选择器。假设您的GridView标识为“taskgrid”,请尝试使用以下选择器:

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

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

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