How to count the rows in a gridview in asp.net using jQuery

前端 未结 4 1426
逝去的感伤
逝去的感伤 2021-01-13 04:54

Does anyone know how to count the no of rows in asp:GridView using jQuery. If no rows found then I want to do stuff...

4条回答
  •  再見小時候
    2021-01-13 05:26

    You can assign a CSS class to your gridview using CssClass (I don't remember the exact spelling) property, and then access it jquery's css class selectors.

    Suppose you have assigned gridviewclass to that property, then when you write -

    $('table.gridviewclass')

    in jquery, you will be able to access the table that is being generated in place of that gridview by ASP.NET. Now, to access all the rows, you will write -

    $('table.gridviewclass tr')
    

    which will give you all the rows of that table inside a jquery array. To count the number of rows, you will then write -

    var rowcount = $('table.gridviewclass tr').length
    
    if(rowcount == 0)
    {
        // No rows found, do your stuff
    }
    else
    {
        // Rows found, do whatever you want to do in this case
    }
    

    To access the first row, you can use the following selector -

    $('table.gridviewclass tr:first')
    

    To access the last row, you will write -

    $('table.gridviewclass tr:last')
    

    etc. You can find a whole list of jquery selectors here.

    Hope that helps.

提交回复
热议问题