Intersoft WebGrid Documentation
Walkthrough: Using CheckBox column in Hierarchical WebGrid
See Also Send comments on this topic.

Glossary Item Box

Mark all the child rows as checked if the CheckBox in the owning parent row is checked. This task is done by using client-side script.

During this walkthrough, you will learn how to do the following:

 Prerequisites

In order to complete this walkthrough, you will need the following:

  • Visual Studio 2005 Application.

 Step-By-Step Instructions

To create new application and mark all the child rows

  1. Binding WebGrid to Hierarchical ISDataSource.
  2. Create a hierarchical grid with one column of each table is set to CheckBox column type and name the columns as 'CheckBoxColumn'.
  3. Attach the 'onclick' attributes to the parent CheckBox column in InitializeRow event.

    C# Copy Code
    private void WebGrid1_InitializeRow(object sender,
    ISNet.WebUI.WebGrid.RowEventArgs e)
    {
        if (e.Row.Table.DataMember == "Customers") 
        {
            e.Row.Cells.GetNamedItem("CheckBoxColumn").CustomObjectAttributes =
                "onclick='doChecking(this)'";
        }
    }
    

  4. Apply the following script to the page:

    JavaScript Copy Code
                    
    var row = null;
    var IsExpandedByCheck = false;
                    
    function doChecking(obj)
    {
        var grid = ISGetObject("WebGrid1");
        row = grid.GetSelectedObject().GetRowObject();
        // expand the row if it's not yet expanded
        if (!row.ChildExpanded)
        {
            IsExpandedByCheck = true;
            row.ExpandChildRow();
        }
        else
        {
            // if it's already expanded..
            MarkChildRows(row, obj.checked);
        }
    }
                    
    // mark the child rows after being expanded
    function WebGrid1_OnAfterResponseProcess(gridId, action, lastRO, xmlRO) {
        if (action == "LoadChild" && IsExpandedByCheck) {
            MarkChildRows(row, true);
            IsExpandedByCheck = false;
            row = null; // reset row
        }
        return true;
    }
    
    function MarkChildRows(row, checked) {
        var childRows = row.GetChildRows(); // get the child rows
        for (var i=0; i<childRows.length; i++) {
            if (childRows[i].GetElement().type == "Record") {
                var cell = wgGetCellByName(childRows[i].GetElement(), "CheckBoxColumn");
                cell.childNodes[0].checked = checked;
            }
        }
    }
                                
    

  5. Register the WebGrid1_OnAfterResponseProcess function to the grid's OnAfterResponseProcess client-side event.

See Also

©2012 Intersoft Solutions Corp. All Rights Reserved.