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

How to get list of id of selected checkbox in radgrid on button click?

13 Answers 1207 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Swapnil
Top achievements
Rank 1
Swapnil asked on 06 Aug 2013, 09:44 AM
Hi,
How to get list of  id of selected checkbox in radgrid on button click?

Source:
<telerik:RadGrid ID="gv1" runat="server">
            <MasterTableView DataKeyNames="UserId">
                <Columns>
                    <telerik:GridTemplateColumn>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

What will be the code on save button click to get the list of UserID releted to the checked checkbox row?
Thanks

13 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 06 Aug 2013, 11:21 AM
Hi Swapnil,

Please have a look into the following code I tried which works fine at my end.

ASPX:
protected void RadButton1_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in gv1.MasterTableView.Items)
    {
        CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
        if (chk.Checked == true)
        {
            string ID = item.GetDataKeyValue("DataKeyNameString").ToString();
        }
    }
}

Thanks,
Shinu.
0
Swapnil
Top achievements
Rank 1
answered on 08 Aug 2013, 04:58 AM
I tried with this code
<telerik:RadGrid ID="gv1" runat="server">
           <MasterTableView DataKeyNames="UserName">
               <Columns>
                   <telerik:GridTemplateColumn>
                       <ItemTemplate>
                           <asp:CheckBox ID="CheckBox1" runat="server" />
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
protected void btnsave_Click(object sender, EventArgs e)
   {
       foreach (GridDataItem item in gv1.MasterTableView.Items)
       {
           CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
           if (chk.Checked == true)
           {
               string ID = item.GetDataKeyValue("UserName").ToString();
           }
       }
   }
but while debugging it is not going inside IF condition and chk value is given as checked="false"
Thanks
0
Swapnil
Top achievements
Rank 1
answered on 08 Aug 2013, 05:01 AM
I tried this but debugger not going inside if condition and value of chk is shown as checked=false even if i checked the checkbox

<telerik:RadGrid ID="gv1" runat="server">
            <MasterTableView DataKeyNames="UserName">
                <Columns>
                    <telerik:GridTemplateColumn>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
and
protected void btnsave_Click(object sender, EventArgs e)
   {
       foreach (GridDataItem item in gv1.MasterTableView.Items)
       {
           CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
           if (chk.Checked == true)
           {
               string ID = item.GetDataKeyValue("UserName").ToString();
           }
       }
   }
0
Shinu
Top achievements
Rank 2
answered on 08 Aug 2013, 12:21 PM
Hi Swapnil,

Unfortunately I couldn't replicate the issue at my end.

Thanks,
Shinu.
0
Fuhai
Top achievements
Rank 1
answered on 08 Aug 2013, 12:40 PM
Hey,

your code looks ok, the check box item in client side maybe not working as expected, try to use this line for clientidmode. and let me know if it is working for you.

<ItemTemplate>

<asp:CheckBox ID="chbAttached" runat="server" ClientIDMode="AutoID" ></asp:CheckBox>

</ItemTemplate>

0
Swapnil
Top achievements
Rank 1
answered on 12 Aug 2013, 04:50 AM
This also doesnt work for me ,i added the line ClientIDMode="AutoID" but it also not doing nothing
main problem is while debugging the code on button click the debugger is not going inside if condition and each time the checkbox checked is given false
protected void btnsave_Click(object sender, EventArgs e)
   {
       foreach (GridDataItem item in gv1.MasterTableView.Items)
       {
           CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
           if (chk.Checked == true)
           {
               string ID = item.GetDataKeyValue("UserName").ToString();
           }
       }
   }

0
Princy
Top achievements
Rank 2
answered on 12 Aug 2013, 06:28 AM
Hi Swapnil,

I have tried the above code and it works fine at my end.See the below code snippet.Can you make sure that you are able to access the checkbox using breakpoints.Can you please provide your full code.

ASPX:

<telerik:RadGrid ID="RadGrid1" runat="server">       
           <MasterTableView DataKeyNames="OrderID">
               <Columns>
                  <telerik:GridTemplateColumn>
                       <ItemTemplate>
                           <asp:CheckBox ID="CheckBox1" runat="server" ClientIDMode="AutoID"></asp:CheckBox>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
       <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />

C#:
protected void Button1_Click(object sender, EventArgs e)
   {
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
     {
       CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
       if (chk.Checked == true)
       {
         string ID = item.GetDataKeyValue("OrderID").ToString();              
       }
     }
   }

Thanks,
Princy
0
Swapnil
Top achievements
Rank 1
answered on 12 Aug 2013, 06:46 AM
Hi Princy,
Same problem not able to figure out
here is my code
<%@ Page Title="" Language="C#" MasterPageFile="~/ReportAdmin.Master" AutoEventWireup="true"
    CodeFile="GroupUserCreation.aspx.cs" Inherits="GroupUserCreation" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <center>
        <telerik:RadComboBox ID="cmbgroupname" runat="server" Width="200px" EmptyMessage="             Select Group"
            Skin="Default">
        </telerik:RadComboBox>
        <br />
        <br />
        <telerik:RadGrid ID="gv1" runat="server">
            <MasterTableView DataKeyNames="UserId">
                <Columns>
                    <telerik:GridTemplateColumn>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" ClientIDMode="AutoID"></asp:CheckBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
        <br />
    </center>
</asp:Content>
and
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Telerik.Web.UI;
using System.Collections;
 
 
public partial class GroupUserCreation : System.Web.UI.Page
{
    Globas obj = new Globas();
    CheckBox chkbox = new CheckBox();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
        }
        DataTable dt = obj.UserDetails();
        gv1.DataSource = dt;
        gv1.DataBind();
 
        DataTable dt1 = obj.GroupDetails();
        cmbgroupname.DataSource = dt1;
        cmbgroupname.DataTextField = "GroupName";
        cmbgroupname.DataValueField = "GroupId";
        cmbgroupname.DataBind();
        
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridDataItem item in gv1.MasterTableView.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
            if (chk.Checked == true)
            {
                string ID = item.GetDataKeyValue("UserId").ToString();
            }
        }
    }
 
     
 
}
Thanks
0
Accepted
Princy
Top achievements
Rank 2
answered on 12 Aug 2013, 07:23 AM
Hi Swapnil,

Please bind the RadGrid using NeedDataSource event and in the page load if you want to bind the RadComboBox,please try it inside the 
if(!IsPostBack).The post back is causing the checkbox to return to its normal state,which is uncheck,that's why,the condition is always false.

Thanks,
Princy
   
0
Swapnil
Top achievements
Rank 1
answered on 12 Aug 2013, 07:46 AM
above code working fine but by using this code i can check all checkboxes
<telerik:RadGrid ID="gv1" runat="server" AllowMultiRowSelection="true" onneeddatasource="RadGrid1_NeedDataSource">
    <ClientSettings Selecting-AllowRowSelect="true">
    </ClientSettings>
    <MasterTableView DataKeyNames="UserId">
        <Columns>       
            <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" />       
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
       <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
and
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
 
           DataTable dt1 = obj.GroupDetails();
           cmbgroupname.DataSource = dt1;
           cmbgroupname.DataTextField = "GroupName";
           cmbgroupname.DataValueField = "GroupId";
           cmbgroupname.DataBind();
       }
     
       
   }
 
   protected void Button1_Click(object sender, EventArgs e)
   {
       foreach (GridDataItem item in gv1.MasterTableView.Items)
       {
           CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
           if (chk.Checked == true)
           {
               int ID =Convert.ToInt32 (item.GetDataKeyValue("UserId").ToString());
           }
       }
   }
    
   protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       DataTable dt = obj.UserDetails();
       gv1.DataSource = dt;
     
   }

but its giving error as object reference not set to an instance of object please give some solution
thnks in advance
0
Swapnil
Top achievements
Rank 1
answered on 12 Aug 2013, 07:57 AM
Thanks for the help Princy
got the solution
0
Siddharth
Top achievements
Rank 1
answered on 13 Aug 2015, 08:18 PM
In the similar scenario , how to get the id of unselected(deselected) row?
0
Eyup
Telerik team
answered on 18 Aug 2015, 09:53 AM
Hello Siddharth,

There are several different approaches you can use in order to achieve this requirement. This can be done either on client-side using the OnRowDeselected event handler or on server-side using the ItemCommand event handler after enabling the EnablePostBackOnRowClick property.

Let me know if you want the solution to be on server or client-side and I will prepare a sample runnable web site for your convenience and send it to you.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Swapnil
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Swapnil
Top achievements
Rank 1
Fuhai
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Siddharth
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or