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

[Solved] Check all chkboxes server side

2 Answers 160 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 12 May 2009, 09:18 AM
For legacy reasons I have a grid with a GridCheckBoxColumn.

I have a button which needs to check all the chekboxes serverside.   Following an example in another thread I coded it this way.  Unfortunately it only produces an indexing error.

Anyone see where I'm going wrong?

Thanks.

Protected Sub btnChkAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnChkAll.Click
        'resize the grid to the maximum number of rows
        RadGridResult.MasterTableView.PageSize = hfGridRows.Value
        RadGridResult.Rebind()
        'check all the checkboxes

        Dim chbox As CheckBox = RadGridResult.MasterTableView.GetItems(GridItemType.CommandItem)(0).FindControl("colCheckBox")
        chbox.Enabled = True

    End Sub

<rad:RadGrid ID="RadGridResult" runat="server" GridLines="None"  EnableAJAX="True" Skin="WebBlue" EnableAJAXLoadingTemplate="True"
         AutoGenerateColumns="False" Height="370px" Width="950px" AllowPaging="True" AllowMultiRowSelection=true>
        
            <MasterTableView>           
                <ExpandCollapseColumn Resizable="False" Visible="False">
                    <HeaderStyle Width="20px" />
                </ExpandCollapseColumn>
                <RowIndicatorColumn Visible="False">
                    <HeaderStyle Width="20px" />
                </RowIndicatorColumn>
                <Columns>                
                    
                    
                    <rad:GridCheckBoxColumn UniqueName="colCheckBox">
                    </rad:GridCheckBoxColumn>
                    <rad:GridButtonColumn ButtonType="ImageButton" UniqueName="colImage">
                        <ItemStyle Width="10px" />
                    </rad:GridButtonColumn>
                    <rad:GridButtonColumn CommandName="LessonID" DataTextField="LessonID" HeaderText="Lesson ID" UniqueName="column">
                    </rad:GridButtonColumn>
                    <rad:GridButtonColumn DataTextField="LearningTitle" HeaderText="Lesson Title" UniqueName="colLearningTitle"
                        CommandArgument="LearningTitle">
                    </rad:GridButtonColumn>
                    <rad:GridBoundColumn DataField="LessonDate" HeaderText="Lesson Date" UniqueName="colLessonDate">
                    </rad:GridBoundColumn>
                    <rad:GridBoundColumn DataField="Discipline" HeaderText="Discipline" UniqueName="colDiscipline">
                    </rad:GridBoundColumn>
                    <rad:GridBoundColumn DataField="Area" HeaderText="Area of Op." UniqueName="colArea">
                    </rad:GridBoundColumn>
                    <rad:GridBoundColumn DataField="Category" HeaderText="Category" UniqueName="colCategory">
                    </rad:GridBoundColumn>
                    <rad:GridBoundColumn DataField="subCategory" HeaderText="Subcategory" UniqueName="colSubCategory">
                    </rad:GridBoundColumn>
                    <rad:GridBoundColumn DataField="Field" HeaderText="Project" UniqueName="column1">
                    </rad:GridBoundColumn>
                    <rad:GridBoundColumn DataField="NIDP" HeaderText="NIDP Stage" UniqueName="column2">
                    </rad:GridBoundColumn>
                  
                </Columns>
                <AlternatingItemStyle Wrap="True" />
            </MasterTableView>
            <PagerStyle Mode=NextPrevNumericAndAdvanced/>            
        </rad:RadGrid>

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 May 2009, 11:17 AM
Hi Mike,

The CheckBox in the GridCheckBoxColumn will be enabled only in edit mode and it will be disabled in normal mode. So I would suggest you to use a GridTemplateColumn with a CheckBox in its ItemTemplate. In the button Click event you can loop through the Grid rows and access the CheckBoxes and then check it as shown below.

ASPX:
 
<rad:GridTemplateColumn UniqueName="CheckTemp" HeaderText="CheckCol" > 
            <ItemTemplate> 
                <asp:CheckBox ID="CheckBox1" runat="server" /> 
            </ItemTemplate> 
          </rad:GridTemplateColumn> 

CS:
 
   protected void Button1_Click(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
        { 
            CheckBox chkbx = (CheckBox)item["CheckTemp"].FindControl("CheckBox1"); 
            chkbx.Checked = !chkbx.Checked; 
        } 
    } 


If you need to achieve this with a GridCheckBoxColumn itself you may try the following code.

ASPX:
 
  <telerik:GridCheckBoxColumn UniqueName="CheckBoxColumn" HeaderText="CheckBoxColumn" ></telerik:GridCheckBoxColumn> 

CS:
 protected void Button2_Click(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
        { 
            CheckBox chkbx = (CheckBox)item["CheckBoxColumn"].Controls[0]; 
            chkbx.Checked = !chkbx.Checked; 
        } 
    } 

Thanks
Shinu
0
Mike
Top achievements
Rank 1
answered on 12 May 2009, 12:55 PM
Thanks Shinu, I am going to give this a try.

Mike
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Mike
Top achievements
Rank 1
Share this question
or