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

binding Checkboxes in template column

4 Answers 241 Views
Grid
This is a migrated thread and some comments may be shown as answers.
3i infotech
Top achievements
Rank 1
3i infotech asked on 06 May 2008, 06:48 AM

Hi Everyone,

How to loop through the bound checkboxes in the template column

I have checkboxes in the radgrid which are bound to a table. They have to be editable.

Later I need to loop through them to find out whether they are checked or not and then depending on that I have to update the database.

Please tell me how to go about this.

The code snippet is as follows:

<telerik:radgrid id="RadGrid1" runat="server" allowpaging="True" gridlines="None" autogeneratecolumns="False" skin="Default2006" showstatusbar="True" >

                <clientsettings allowcolumnsreorder="True" reordercolumnsonclient="True">

                </clientsettings>

                <mastertableview datakeynames="MenuSysID" commanditemdisplay="Top">

                <commanditemtemplate>

                    <div style="padding: 10px 0px;">

                        <asp:linkbutton runat="server" id="lnkEdit" commandname="Save">

              <img style="border:0px;vertical-align:middle;" alt="" src="Img/icon_edit.gif" />

              Save

                        </asp:linkbutton>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                </commanditemtemplate>

                    <columns>

                        <telerik:gridtemplatecolumn datafield="MenuCaption" forceextractvalue="Always" headertext="Menu"

                            uniquename="MenuCaption">

                            <itemtemplate>

                                <asp:label id="MenuCaptionLabel" runat="server" text='<%# Eval("MenuCaption") %>'></asp:label>

                            </itemtemplate>

                        </telerik:gridtemplatecolumn>

                        <telerik:gridtemplatecolumn headertext="Input" uniquename="TemplateColumn1">

                            <itemtemplate>

                                <asp:checkbox id="CheckBox1" runat="server" checked='<%# Eval("Input") %>' />

                            </itemtemplate>

                            <edititemtemplate>

                            <asp:checkbox id="CheckBox1" runat="server" checked='<%# Eval("Input") %>' />

                            </edititemtemplate>

                        </telerik:gridtemplatecolumn>

                        <telerik:gridtemplatecolumn headertext="Approve" uniquename="TemplateColumn2">

                            <itemtemplate>

                                <asp:checkbox id="CheckBox2" runat="server" checked='<%# Eval("Approve") %>' />

                            </itemtemplate>

                            <edititemtemplate>

                            <asp:checkbox id="CheckBox2" runat="server" checked='<%# Eval("Approve") %>' />

                            </edititemtemplate>

                        </telerik:gridtemplatecolumn>

                        <telerik:gridtemplatecolumn headertext="Delete" uniquename="TemplateColumn3">

                            <itemtemplate>

                                <asp:checkbox id="CheckBox3" runat="server" checked='<%# Eval("Delete") %>' />

                            </itemtemplate>

                            <edititemtemplate>

                            <asp:checkbox id="CheckBox3" runat="server" checked='<%# Eval("Delete") %>' />

                            </edititemtemplate>

                        </telerik:gridtemplatecolumn>

                        <telerik:gridtemplatecolumn headertext="View" uniquename="TemplateColumn">

                            <itemtemplate>

                                <asp:checkbox id="CheckBox4" runat="server" checked='<%# Eval("View") %>' />

                            </itemtemplate>

                            <edititemtemplate>

                            <asp:checkbox id="CheckBox4" runat="server" checked='<%# Eval("View") %>' />

                            </edititemtemplate>

                        </telerik:gridtemplatecolumn>

                    </columns>

                  

                    <expandcollapsecolumn resizable="False" visible="False">

                        <headerstyle width="20px" />

                    </expandcollapsecolumn>

                    <rowindicatorcolumn visible="False">

                        <headerstyle width="20px" />

                    </rowindicatorcolumn>

                </mastertableview>

            </telerik:radgrid>

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 06 May 2008, 07:26 AM
Hi,

Try the following code snippet to access a CheckBox in the ItemTemplate of a GridTemplateColumn.

CS:
protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
        { 
            CheckBox chkbx = (CheckBox)item["TemplateColumn1"].FindControl("CheckBox1"); 
            if (chkbx.Checked) 
            { 
            //... 
            } 
            else 
            {  
              //... 
            } 
        } 
        
      
    } 


Thanks
Shinu.
0
3i infotech
Top achievements
Rank 1
answered on 06 May 2008, 10:43 AM
Hi

Actually i need to loop through the checkboxes just before the insert command fires since i need to update my database based on whether the checkboxes are checked or unchecked. Pre-Render fires after page load but before the insert command, it doesnt fire. Hence not much use for me as i dont get the updated values of checkbox i.e. the checkbox can be checked or unchecked also.
So the new values have to be picked up.
0
Nikita Gourme
Top achievements
Rank 1
answered on 06 May 2008, 11:04 AM
I checked your code and here is my advise:

1) You have duplicate ids for the checkboxes in the item/edit templates. Consider providing unique value for each checkbox to avoid unexpected results.
2) Use Bind("ColumnName") expression instead of Eval for the checkboxes inside the edit templates. Thus you will have two-way binding and will be able to get the checked state change on update/insert.
2) To reference the checkboxes on update/insert command, use the FindControl(id) method, for example.:

(e.Item.FindControl("CheckBox3") as CheckBox).Checked

Nikita
0
3i infotech
Top achievements
Rank 1
answered on 06 May 2008, 11:04 AM
same code worked in update command..Thanks
Tags
Grid
Asked by
3i infotech
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
3i infotech
Top achievements
Rank 1
Nikita Gourme
Top achievements
Rank 1
Share this question
or