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

checkbox in grid

1 Answer 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Harry
Top achievements
Rank 1
Harry asked on 13 Apr 2010, 06:27 AM

hello.. i m using telerik grid..grid havin three checkbox  in threee columns..i want user can be able to select single checkbox at a time..

  <himani:HimaniGrid ID="EmployeeAttendanceGrid" runat="server" GridLines="None"   
        AutoGenerateColumns="False"   
           onitemcommand="EmployeeAttendanceGrid_ItemCommand"   
           onitemdatabound="EmployeeAttendanceGrid_ItemDataBound">  
<MasterTableView cellspacing="-1">  
<RowIndicatorColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</RowIndicatorColumn> 
<ExpandCollapseColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</ExpandCollapseColumn> 
<Columns> 
<himani:GridTemplateColumn Visible="false">  
<ItemTemplate> 
    <asp:Label ID="LbEmployeeid" runat="server" Text='<%#Eval("ID")%>'></asp:Label> 
</ItemTemplate> 
</himani:GridTemplateColumn> 
<himani:GridTemplateColumn HeaderText="Employee Name">  
<ItemTemplate> 
    <asp:Label ID="Label2" runat="server" Text='<%#Eval("NAME")%>'></asp:Label> 
</ItemTemplate> 
</himani:GridTemplateColumn> 
 
<himani:GridTemplateColumn HeaderText="Present" > 
<ItemTemplate> 
 <asp:CheckBox ID="ChkPresent" runat="server" /> 
 
</ItemTemplate> 
</himani:GridTemplateColumn> 
<himani:GridTemplateColumn HeaderText="Absent" > 
 
<ItemTemplate> 
    <asp:CheckBox ID="ChkAbsent" runat="server" /> 
</ItemTemplate> 
</himani:GridTemplateColumn> 
<himani:GridTemplateColumn HeaderText="Leave" > 
<ItemTemplate> 
    <asp:CheckBox ID="Chkleave" runat="server" /> 
</ItemTemplate> 
</himani:GridTemplateColumn> 
<himani:GridTemplateColumn> 
<ItemTemplate> 
    <asp:Button ID="btnView" runat="server" Text="View" /> 
</ItemTemplate> 
</himani:GridTemplateColumn> 
</Columns> 
</MasterTableView> 
    </himani:HimaniGrid> 
 
<script language="javascript" type="text/javascript">  
        function check(ChkPresent,ChkAbsent, Chkleave, currentCheckBoxClicked) {  
            var objC1 = document.getElementById(ChkPresent);  
            var objC2 = document.getElementById(ChkAbsent);  
            var objC3 = document.getElementById(Chkleave);  
              
            if (ChkPresent== currentCheckBoxClicked) {  
                objC2.checked = false;  
                objC3.checked = false;  
            }  
            else if (ChkAbsent == currentCheckBoxClicked) {  
                objC1.checked = false;  
                objC3.checked = false;  
            }  
            else if (Chkleave == currentCheckBoxClicked) {  
                objC1.checked = false;  
                objC2.checked = false;  
            }  
        }  
    </script> 
 
 
 
 protected void EmployeeAttendanceGrid_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        //GridDataItem item = (GridDataItem)(((CheckBox)e.CommandSource).NamingContainer);  
        if(e.Item.ItemType==Himani.Web.UI.GridItemType.AlternatingItem)  
        //if (e.Row.RowType == DataControlRowType.DataRow)  
       {  
            CheckBox c1 = e.Item.FindControl("ChkPresent") as CheckBox;  
            CheckBox c2 = e.Item.FindControl("ChkAbsent") as CheckBox;  
            CheckBox c3 = e.Item.FindControl("Chkleave") as CheckBox;  
 
            c1.Attributes.Add("onclick", "javascript:check('" + c1.ClientID + "','" + c2.ClientID + "','" + c3.ClientID + "',this.id);");  
            c2.Attributes.Add("onclick", "javascript:check('" + c1.ClientID + "','" + c2.ClientID + "','" + c3.ClientID + "',this.id);");  
            c3.Attributes.Add("onclick", "javascript:check('" + c1.ClientID + "','" + c2.ClientID + "','" + c3.ClientID + "',this.id);");  
        }  
 
    }  
 
please reply....in one row i able to select  one checkbox..
in second row it creating problem i able can select all checkbox

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 13 Apr 2010, 09:06 AM
Hello Harry,

Your code works fine with a small change in code. Check for GridDataItem in ItemDataBound event than AlternatingItem.

CS:
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    {        
        if (e.Item is GridDataItem
        { 
            CheckBox c1 = e.Item.FindControl("ChkPresent"as CheckBox; 
            CheckBox c2 = e.Item.FindControl("ChkAbsent"as CheckBox; 
            CheckBox c3 = e.Item.FindControl("Chkleave"as CheckBox; 
 
            c1.Attributes.Add("onclick""javascript:check('" + c1.ClientID + "','" + c2.ClientID + "','" + c3.ClientID + "',this.id);"); 
            c2.Attributes.Add("onclick""javascript:check('" + c1.ClientID + "','" + c2.ClientID + "','" + c3.ClientID + "',this.id);"); 
            c3.Attributes.Add("onclick""javascript:check('" + c1.ClientID + "','" + c2.ClientID + "','" + c3.ClientID + "',this.id);"); 
        } 
    } 

Regards,
Princy.
Tags
Grid
Asked by
Harry
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or