This is a migrated thread and some comments may be shown as answers.

GridClientSelectColumn header checkbox selects disabled rows

8 Answers 365 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Margo Noreen
Top achievements
Rank 1
Veteran
Margo Noreen asked on 04 Apr 2012, 10:20 PM
I added a GridClientSelectColumn column to my RadGrid.  On the ItemDataBound event, I disable that checkbox depending on certain other values of the row.  So the checkbox is correctly rendered as disabled.  However, the checkbox in the grid's header, that allows the user to select/deselect all ignores that state of the checkbox. 

So, if I click the checkbox in the header, it checks all the checkboxes, even if they are disabled.

Is there a way around this?  Or will I have to manually put in my own checkbox column, javascript, etc.  to manage it?

8 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Apr 2012, 06:41 AM
Hi Margo Noreen,

You can make the disabled CheckBoxes uncheck, while checking the header CheckBox using Javascript. Please take a look into the following code.

ASPX:
<ClientSettings>
<ClientEvents OnRowSelecting="RowSelecting"/>
</ClientSettings>

Javascript:
function RowSelecting(sender, args)
{
    var id = args.get_id();
    var inputCheckBox = $get(id).getElementsByTagName("input")[0];
    if (!inputCheckBox || inputCheckBox.disabled)
    {
        //cancel selection for disabled rows
        args.set_cancel(true);
    }
}

Hope this helps.
Thanks,
Shinu.
0
Margo Noreen
Top achievements
Rank 1
Veteran
answered on 05 Apr 2012, 02:49 PM
Thank you, that worked for my purposes.
0
Robert
Top achievements
Rank 1
answered on 18 Sep 2012, 05:26 PM
s
0
Robert
Top achievements
Rank 1
answered on 18 Sep 2012, 06:06 PM
This doesn't work.  It will select and "check" the enabled items when you click the header checkbox. However the header checkbox remains unchecked and if you attempt to click it again it is only checking the enabled items.

If you then uncheck a few rows and then click the header checkbox it will select those unchecked rows again.  It seems as if the selecting of the items is working to some extent but the unchecking is not working at all.

Any ideas?  I tested in IE9 and Chrome with the same results.

0
Shinu
Top achievements
Rank 2
answered on 19 Sep 2012, 04:01 AM
Hi Robert,

Please try the following code snippet.

ASPX:
<telerik:RadGrid>
   . . . . . . . . . . . . . . . . . .
    <ClientSettings>
       <ClientEvents OnRowSelecting="RowSelecting"/>
       <Selecting AllowRowSelect="true" />
    </ClientSettings>
 
       . . . . . . . . . . . . . . . . . . .
 </telerik:RadGrid>
  
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HiddenField2" runat="server" />

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridHeaderItem)
    {
        GridHeaderItem headerItem = (GridHeaderItem)e.Item;
        CheckBox chk = (CheckBox)headerItem["ClientSelectColumn"].Controls[0];
        HiddenField1.Value = chk.ClientID;
    }
}

Javascript:
<script type="text/javascript">
    var selecting;
    var grid;
    var checkBox;
    function pageLoad() {
        checkBox = $get($get('<%= HiddenField1.ClientID %>').value)
        $addHandler(checkBox, "click", onclickHandler);
        grid = $find('<%=RadGrid1.ClientID %>')
        var tmp = grid.get_masterTableView().get_dataItems().length;
        if (parseInt($get('<%= HiddenField2.ClientID %>').value, 10) + grid.get_masterTableView().get_selectedItems().length ==             grid.get_masterTableView().get_dataItems().length) {
            checkBox.checked = true;
        }
    }
    function onclickHandler() {
        if (grid.get_masterTableView().get_selectedItems().length > 0) {
            checkBox.checked = true;
        }
        else {
            checkBox.checked = false;
        }
    }
    var selecting = true;
    function RowSelecting(sender, args) {
        var id = args.get_id();
        var inputCheckBox = $get(id).getElementsByTagName("input")[0];
        if (!inputCheckBox || inputCheckBox.disabled) {
            //cancel selection for disabled rows
            args.set_cancel(true);
        }
        // if no more unselected enabled rows left - check the header checkbox
        else if (parseInt($get('<%= HiddenField2.ClientID %>').value, 10) + grid.get_masterTableView().get_selectedItems().length + 1 ==                  grid.get_masterTableView().get_dataItems().length) {
            checkBox.checked = true;
        }
    }
</script>

Thanks,
Shinu.
0
Robert
Top achievements
Rank 1
answered on 19 Sep 2012, 12:57 PM
Thanks Shinu,

That seems to be working for me.

Robert
0
Albert Shenker
Top achievements
Rank 1
Veteran
Iron
answered on 07 Jul 2020, 05:46 PM
If you don't need to both select and disable a particular row at the same time, then setting  e.Item.SelectableMode = GridItemSelectableMode.None in the ItemDatabound event seems to do the trick with far less code.
0
Doncho
Telerik team
answered on 10 Jul 2020, 08:31 AM

Hi Albert,

Thank you for sharing that approach with the community it may help other developers in a such scenario.

Kind regards,
Doncho
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Grid
Asked by
Margo Noreen
Top achievements
Rank 1
Veteran
Answers by
Shinu
Top achievements
Rank 2
Margo Noreen
Top achievements
Rank 1
Veteran
Robert
Top achievements
Rank 1
Albert Shenker
Top achievements
Rank 1
Veteran
Iron
Doncho
Telerik team
Share this question
or