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

Custom Checkbox Template In DataGrid

1 Answer 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Shawn
Top achievements
Rank 1
Shawn asked on 19 May 2009, 10:37 PM
I have a checkbox column in my datagrid like so:
<Columns> 
        <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn"
            <HeaderTemplate> 
                <asp:CheckBox ID="cl" onclick="return check_uncheck (this );" runat="server" /> 
            </HeaderTemplate> 
            <ItemTemplate> 
                <asp:CheckBox ID="chk_record" onclick="return check_uncheck (this );" runat="server" /> 
            </ItemTemplate> 
        </telerik:GridTemplateColumn> 
        <telerik:GridBoundColumn DataField="record"  
            HeaderText="record" SortExpression="record"  
            UniqueName="record" DataType="System.Int32" ReadOnly="True"  
            Visible="False"
        </telerik:GridBoundColumn> 
<script type="text/javascript"
    function check_uncheck(Val) { 
        var ValChecked = Val.checked; 
        var ValId = Val.id; 
        var frm = document.forms[0]; 
        // Loop through all elements 
        for (i = 0; i < frm.length; i++) { 
            // Look for Header Template's Checkbox 
            //As we have not other control other than checkbox we just check following statement 
            if (this != null) { 
                if (ValId.indexOf('cl') != -1) { 
                    // Check if main checkbox is checked, 
                    // then select or deselect datagrid checkboxes 
                    if (ValChecked) 
                        frm.elements[i].checked = true
                    else 
                        frm.elements[i].checked = false
                } 
                else if (ValId.indexOf('deleteRec') != -1) { 
                    // Check if any of the checkboxes are not checked, and then uncheck top select all checkbox 
                    if (frm.elements[i].checked == false
                        frm.elements[1].checked = false
                } 
            } // if 
        } // for 
    } // function 
 
</script> 

I want the ability to run a number of different functions on this page which will use the data specified by the checkbox boolean.  I already have the grid using its row click selected event to load the nested view details, what I want now is the ability to get the row records associated with the checkboxes when something like a link button is clicked.

So how do I get the record integer value of all the checkboxes that are selected when my link button is clicked?
 Protected Sub LinkButton1_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles LinkButton1.Click 
 
'how do I loop through all of the checkboxes and get the row key (record) for all of them that are selected? 
 
    End Sub 

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 20 May 2009, 05:12 AM
Hi Shawn,

Try the following code snippet.

VB:
 
Protected Sub LinkButton1_Click(ByVal sender As ObjectByVal e As EventArgs) 
    For Each item As GridDataItem In RadGrid1.MasterTableView.Items 
        Dim chk As CheckBox = DirectCast(item.FindControl("chk_record"), CheckBox) 
        If chk.Checked Then 
                'item["record"].Text will represent the value assosiated with checked Checkbox 
            Response.Write(item("record").Text) 
        End If 
    Next 
End Sub 
 
Hope this helps :)

Thanks,
Shinu.
Tags
Grid
Asked by
Shawn
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or