Check All CheckBoxes In GridView Via JavaScript Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    Check All CheckBoxes In GridView Via JavaScript Problem

    I'm working on an ASP.NET application using VB.NET for server side code.

    I have a GridView listing a bunch of stuff. Above the GridView I have a checkbox named "ChkBx_SelectAl l". If this checkbox is checked, all of the rows in the GridView are checked, if this checkbox is unchecked, all of the rows in the GridView are unchecked.

    This checking/unchecking of the checkboxes in the GridView is handled by a JavaScript method:

    [code=javascript]
    function checkAllCheckbo xes(idOfControl lingCheckbox,gr idViewClientID)
    {
    var grid = document.getEle mentById(gridVi ewClientID);
    var cell;

    if (grid.rows.leng th > 0)
    {
    for (i=0; i<grid.rows.len gth; i++)
    {
    cell = grid.rows[i].cells[0];
    for (j=0; j<cell.childNod es.length; j++)
    {
    if (cell.childNode s[j].type =="checkbox")
    {
    cell.childNodes[j].checked = document.getEle mentById(idOfCo ntrollingCheckb ox).checked;
    }
    }
    }
    }
    }[/code]

    This method is passed the ClientID of the "ChkBx_SelectAl l" checkbox, and sets all of the checkboxes in the GridView (whose ClientID is passed into this method as well) to match the "ChkBx_SelectAl l" checkbox.

    All is well and happy!

    Except......... .....after Unchecking all of the CheckBoxes in my GridView (via the JavaScript method) and then I click a any checkbox(es) in the GridView, that CheckBox is not marked as checked when I'm determining which GridView checkboxes are checked on the server.

    If I remove this code and manually uncheck everything by hand, then check any checkbox(es) everything is fine.

    Does anyone know why this JavaScript method is forcing my GridView to act in this way?

    -Frinny
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I solved this problem earlier today.

    The problem was not with the above code, this code works fine for selecting all checkboxes in a GridView. My problem was that originally this was done server side using Ajax calls, I forgot to remove the CheckChanged event for the Select All checkbox...so this was getting fired every time my Select All checkbox was changing, which effectively removed or set all the checkboxes in my GridView before I could loop through to discover which were checked.

    Lesson of the day:

    "Do Not keep any redundant code"

    -Frinny

    Comment

    Working...