This is a migrated thread and some comments may be shown as answers.
getting the RadGrid checkbox column value in server
15 Answers 236 Views
This is a migrated thread and some comments may be shown as answers.
Arvind
Top achievements
Rank 1
Arvind asked on 26 Jul 2012, 02:31 PM

HI,

I have a RadGrid having a checkbox column, i have added the column as a ItemTemplate to make it editable in the regular mode.

<telerik:GridTemplateColumn UniqueName="IsSelected" DataField="IsSelected">
    <ItemTemplate>
       <asp:CheckBox ID="chkBoolean" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsSelected")) %>'
       Enabled='<%# Convert.ToBoolean(Eval("IsSelectionDisable")) %>' />
    </ItemTemplate>
</telerik:GridTemplateColumn>

Using this I am able to display a checkbox as editable. Now my problem is how do i get the checked value of the checkox for saving it  when the user has changed the checkbox. On click of a button i need to get all the rows that are still checked and save them.

Thanks
Arvind

15 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 26 Jul 2012, 06:46 PM
Hello,

protected void Button1_Click(object sender, EventArgs e)
       {
           foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
           {
               CheckBox chkBoolean = item.FindControl("chkBoolean") as CheckBox;
               // access your checkbox here
           }
       }


Thanks,
Jayesh Goyani
0
Peter
Top achievements
Rank 1
answered on 22 Oct 2014, 05:34 AM
How do I do this in the UpdateCommand event?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Oct 2014, 07:38 PM
Hi Peter,

Can you please provide your code snippet?

Thanks,
Jayesh Goyani
0
Mohammed BENKHDAR
Top achievements
Rank 1
answered on 10 Feb 2015, 10:53 AM
Hello I'm using the telerik version : 2014.3.1209.40.

I'm creating a dynamic radgrid with many templatecolumn containing checkboxes. CheckBox!!

I can set the values of checkboxes in ItemDatabound, but when I change the values and clique on validate Button, I cant find the checkboxes using the same code as in ItemDataBound,  "MyCheckbox" is always NULL

Can you tell what is the wrong ?
CheckBox myCheckBox = new CheckBox();
                foreach (GridDataItem myItem in RadGridSubscription.MasterTableView.Items)
                {
                        for (var i = 2; i < RadGridSubscription.Columns.Count - 1; ++i)
                        {
                            myCheckBox = myItem.FindControl(RadGridSubscription.MasterTableView.Columns[i].UniqueName) as CheckBox;
                            var job = int.Parse(RadGridSubscription.Columns[i].UniqueName.Replace("J", ""));
                            var service = int.Parse(myItem.Cells[3].Text.Replace("S", ""));
 
                            if (myCheckBox!= null && myCheckBox.Checked)
                            {
                                 job = int.Parse(RadGridSubscription.Columns[i].UniqueName.Replace("J", ""));
                            }
                            else
                            {
 
                            }
                        }
                }

 
0
Konstantin Dikov
Telerik team
answered on 13 Feb 2015, 07:57 AM
Hi Mohammed,

In the provided code snippet your are searching for controls with ID equal to the column names. Can you please confirm that your CheckBox controls have the same ID as the columns UniqueName

Furthermore, before using the FindControl method, you may have to get reference to the TableCell first:
myCheckBox = myItem[RadGridSubscription.MasterTableView.Columns[i].UniqueName].FindControl("Check_Box_ID") as CheckBox;

If the above does not help, please open a regular support ticket and provide the markup and the code-behind of your page, so we can have a better idea of your exact implementation.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Mohammed BENKHDAR
Top achievements
Rank 1
answered on 17 Feb 2015, 05:16 PM
Hi Dikov,

Thank you for responding to my request.

The ID of myCheckbox has the same name as UniqueName, I have tried your line code, but I have the same issue.

I will try to open the ticket because my licence have expired and we are processing to renew it.

Regards,
Mohammed
Dassault Systemes
0
Konstantin Dikov
Telerik team
answered on 20 Feb 2015, 11:39 AM
Hi Mohammed,

I would be really more productive if you open a regular support ticket and attach the page in question, so we can examine it locally.

However, if you decide that we will handle the issue here, please provide a simplified version of your code-behind, so we can take a look at it. 

On a side note, can you please ensure that you are creating the CheckBox controls on each postback?


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Mohammed BENKHDAR
Top achievements
Rank 1
answered on 20 Feb 2015, 01:27 PM
Hi Dikov,

Thank you a lot. I will open a ticket as soon as our budget team validate the renewal for the licence.

Actually I will sent the all code page .cs and .ascx, and hope it wlil be enough for to investigate.

Regards
Mohammed
Dassault Systemes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Threading;
using Telerik.Web.UI;
using CommunicationModel;
using System.Net.NetworkInformation;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.UI;
 
 
namespace InputTool.Pages
{
    public partial class Subscriptions : System.Web.UI.UserControl
    {
        private String connStr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
        private static string Filter; //set as static because DataObjectSource doesn't call a specific instance of List to bind.
 
        protected void Page_Load(object sender, EventArgs e)
        {
            //RefreshB.Style["background-image"] = "url('" + System.Web.Configuration.WebConfigurationManager.AppSettings["ImageAbsolutePath"] + "ITIM/button-refresh.png')";
            //Button1.Style["background-image"] = "url('" + System.Web.Configuration.WebConfigurationManager.AppSettings["ImageAbsolutePath"] + "ITIM/button-refresh.png')";
 
             
        }
 
 
        protected void RadGridSubscription_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            fillGrid(false);
        }
 
        // Fill the checkboxes values
        protected void RadGridSubscription_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                DataRowView dr = (DataRowView)e.Item.DataItem;
                CheckBox myCheckBox = new CheckBox();
                GridDataItem myItem = e.Item as GridDataItem;
                if (myItem != null)
                {
                    for (var i = 2; i < RadGridSubscription.Columns.Count - 1; ++i)
                    {
                        myCheckBox = (CheckBox)e.Item.FindControl(RadGridSubscription.Columns[i].UniqueName);
                        var job = int.Parse(RadGridSubscription.Columns[i].UniqueName.Replace("J",""));
                        var service = int.Parse(dr[0].ToString());
 
                        myCheckBox.Checked = MySubscriptionData.GetJobsServiceAssociation(job,service);// if I found the relation between the job and service, the result will be TRUE
                        if (myCheckBox.Checked)
                        {
                            // If it's checked U change the background color
                            myCheckBox.BackColor = Color.LightBlue;
                        }
                    }
                }
            }
        }
 
        //Create the Radgrid dynimically
        public void fillGrid(bool performDataBind)
        {
                DataTable dt = new DataTable();
                // get the services for the first columns
                dt = MySubscriptionData.GetServices();
 
                //clear the grid after each postback
                RadGridSubscription.Columns.Clear();
                RadGridSubscription.DataSource = dt;
                RadGridSubscription.MasterTableView.DataKeyNames = new string[] { "ID" };
 
                //CREATE THE FIRST COLUMN
                GridBoundColumn boundColumn;
                boundColumn = new GridBoundColumn();
                RadGridSubscription.MasterTableView.Columns.Add(boundColumn);
                boundColumn.DataField = "serviceName";
                boundColumn.HeaderText = "Affected Services";
                boundColumn.UniqueName = "service";
                boundColumn.AllowFiltering = true;
                boundColumn.CurrentFilterFunction = GridKnownFunction.Contains;
                boundColumn.ItemStyle.Width = 80;
                boundColumn.ItemStyle.BackColor = Color.LightGray;
                 
                //Create the hiden column for the ID
                boundColumn = new GridBoundColumn();
                RadGridSubscription.MasterTableView.Columns.Add(boundColumn);
                boundColumn.DataField = "ID";
                boundColumn.HeaderText = "service ID";
                boundColumn.UniqueName = "serviceID";
                boundColumn.Display = false;
 
                // now I will create dynamic Jobs columns containing check boxes
                dt = MySubscriptionData.GetJobs();
                GridTemplateColumn templateColumn = new GridTemplateColumn();
                 
                // for each Job I creat a column
                foreach (DataRow dr in dt.Rows)
                {
                    templateColumn = new GridTemplateColumn();
                    templateColumn.ItemTemplate = new MyTemplate(dr[2].ToString());
                    templateColumn.HeaderText = dr[1].ToString();
                    templateColumn.UniqueName = dr[2].ToString();
                    RadGridSubscription.MasterTableView.Columns.Add(templateColumn);
                }
                  
                //Create the last fixed column to force subscription
                templateColumn = new GridTemplateColumn();
                templateColumn.ItemTemplate = new MyTemplate("ForceSubscription");
                templateColumn.HeaderText = "Force subscription to custom user settings";
                templateColumn.UniqueName = "ForceSubscription";
                RadGridSubscription.MasterTableView.Columns.Add(templateColumn);
                templateColumn.ItemStyle.BackColor = Color.LightGray;
                
                if (performDataBind)
                    RadGridSubscription.DataBind();
 
        }
 
        // the checkbox telpmate
        private class MyTemplate : ITemplate
        {
            public CheckBox myCheckBox;
            private string colname;
            public MyTemplate(string cName)
            {
                colname = cName;
            }
            public void InstantiateIn(System.Web.UI.Control container)
            {
                myCheckBox = new CheckBox();
                myCheckBox.ID = colname;
               //myCheckBox. = true;
               // myCheckBox.DataBinding += new EventHandler(boolValue_DataBinding);
                container.Controls.Add(myCheckBox);
            }
 
            //void boolValue_DataBinding(object sender, EventArgs e)
            //{
            //    CheckBox cBox = (CheckBox)sender;
            //    GridDataItem container = (GridDataItem)cBox.NamingContainer;
            //    //cBox.Checked = (bool)((DataRowView)container.DataItem)["Bool"];
            //}
 
        }
 
        protected void btnCancel1_Click(object sender, EventArgs e)
        {
            fillGrid(true);
        }
        //
        //Her I have the issue when I want save the grid to save changes, I need to get each checkbox value and update the database
        //
        protected void btnSave1_Click(object sender, EventArgs e)
        {
            if(RadGridSubscription.Items.Count > 0)
            {
                CheckBox myCheckBox = new CheckBox();
                foreach (GridDataItem myItem in RadGridSubscription.MasterTableView.Items)
                {
                        for (var i = 2; i < RadGridSubscription.Columns.Count - 1; ++i)
                        {
                            myCheckBox = myItem[RadGridSubscription.MasterTableView.Columns[i].UniqueName].FindControl(RadGridSubscription.MasterTableView.Columns[i].UniqueName) as CheckBox;
                            var job = int.Parse(RadGridSubscription.Columns[i].UniqueName.Replace("J", ""));
                            var service = int.Parse(myItem.Cells[3].Text.Replace("S", ""));
 
                            if (myCheckBox!= null && myCheckBox.Checked)
                            {
                                 job = int.Parse(RadGridSubscription.Columns[i].UniqueName.Replace("J", ""));
                            }
                            else
                            {
 
                            }
                        }
                }
                //DataRowView dr = (DataRowView)e.Item.DataItem;
             
                //GridDataItem myItem = e.Item as GridDataItem;
                fillGrid(true);
            }
        }
    }
}
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Subscriptions.ascx.cs" Inherits="InputTool.Pages.Subscriptions" %>
 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
 
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" UpdateInitiatorPanelsOnly="true">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="ConfiguratorPanel">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGridSubscription" LoadingPanelID="RadAjaxLoadingPanel1" />
                <telerik:AjaxUpdatedControl ControlID="ConfiguratorPanel" />
            </UpdatedControls>
        </telerik:AjaxSetting>
        <telerik:AjaxSetting AjaxControlID="RadGridSubscription">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGridSubscription" LoadingPanelID="RadAjaxLoadingPanel1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server"></telerik:RadAjaxLoadingPanel>
 
<asp:Button ID="btnSave1" runat="server" Text="Save"
    onclick="btnSave1_Click"  />
<asp:Button ID="btnCancel1" runat="server" Text="Cancel"
    onclick="btnCancel1_Click"  />
<br />
<br />
 
<telerik:RadGrid ID="RadGridSubscription" runat="server" GridLines="Both"
            AllowSorting="True" AllowPaging="False" Height="600px" AutoGenerateColumns="false"
            OnNeedDataSource="RadGridSubscription_NeedDataSource" OnItemDataBound="RadGridSubscription_ItemDataBound"
            EnableViewState="true">
    <ClientSettings>
        <Scrolling AllowScroll="true" UseStaticHeaders="True" SaveScrollPosition="true" FrozenColumnsCount="1"></Scrolling>
    </ClientSettings>
</telerik:RadGrid>
 
<br />
<br />
<asp:Button ID="btnSave2" runat="server" Text="Save" onclick="btnSave1_Click" />
<asp:Button ID="btnCancel2" runat="server" Text="Cancel" onclick="btnCancel1_Click"/>
            




0
Konstantin Dikov
Telerik team
answered on 25 Feb 2015, 08:05 AM
Hello Mohammed,

The issue that you are experiencing is due to the fact that the OnItemCreated event is firing before your button's click event and the check box will be recreated once again. If you need to edit the values in the check box controls, you will have to put all items in edit mode and traverse the EditItems collection when you need to retrieve the new values.

You can take a look at the following help article for more details on this matter:
Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Mohammed BENKHDAR
Top achievements
Rank 1
answered on 19 May 2015, 08:53 AM

Hello All,

 Thank you for your help, I have found the solution or my real issue.

 I was loanding dynamically my grid on Onload method but it should be done on OnpreRender Methos and it works fine.

0
Jogender
Top achievements
Rank 1
answered on 08 Nov 2016, 06:18 AM
how to save multiple checkbox selected value in database with comma
0
Eyup
Telerik team
answered on 11 Nov 2016, 06:22 AM
Hello Jogender,

You can use the UpdateCommand or ItemCommand to achieve this requirement:
http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/manual-crud-operations/defaultcs.aspx

In addition, you can also check the attached web site sample.

Regards,
Eyup
Telerik by Progress
Check out the new UI for ASP.NET Core, the most complete UI suite for ASP.NET Core development on the market, with 60+ tried-and-tested widgets, based on Kendo UI.
0
ramesh
Top achievements
Rank 1
answered on 19 Dec 2018, 12:21 PM

I am facing exactly the same issue. All of my dynamically loaded checkbox controls are not retrieved when parsing through the items on button click event. It is not the case for other controls. 

This happens only when i try to find the checkbox control. I have same column UniqueName and Checkbox ID. Still getting NULL value every time.

Can you please explain in detail how you exactly rectified this issue? [Any sample code would be of huge help.] 

0
ramesh
Top achievements
Rank 1
answered on 19 Dec 2018, 01:31 PM

https://www.telerik.com/forums/get-checkbox-control-added-dynamically-in-gridtemplate-column

Thank you very much for the link. Made my day. Solved my issue.

0
Eyup
Telerik team
answered on 20 Dec 2018, 08:44 AM
Hello Ramesh,

I'm glad you've managed to resolve the issue and thank you for sharing this link with our community. I hope it will prove helpful to other developers as well.

Regards,
Eyup
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Arvind
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Peter
Top achievements
Rank 1
Mohammed BENKHDAR
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Jogender
Top achievements
Rank 1
Eyup
Telerik team
ramesh
Top achievements
Rank 1
Share this question
or