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

14 posts, 1 answers
  1. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 06 Aug 2013 Link to this post

    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
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 06 Aug 2013 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 07 Aug 2013 Link to this post

    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
  5. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 08 Aug 2013 Link to this post

    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();
               }
           }
       }
  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 Aug 2013 Link to this post

    Hi Swapnil,

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

    Thanks,
    Shinu.
  7. Fuhai
    Fuhai avatar
    6 posts
    Member since:
    Jul 2013

    Posted 08 Aug 2013 Link to this post

    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>

  8. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 11 Aug 2013 Link to this post

    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();
               }
           }
       }

  9. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 12 Aug 2013 Link to this post

    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
  10. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 12 Aug 2013 Link to this post

    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
  11. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 12 Aug 2013 Link to this post

    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
       
  12. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 12 Aug 2013 Link to this post

    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
  13. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 12 Aug 2013 Link to this post

    Thanks for the help Princy
    got the solution
  14. Siddharth
    Siddharth avatar
    1 posts
    Member since:
    Aug 2015

    Posted 13 Aug 2015 Link to this post

    In the similar scenario , how to get the id of unselected(deselected) row?
  15. Eyup
    Admin
    Eyup avatar
    3012 posts

    Posted 18 Aug 2015 Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017