getting the RadGrid checkbox column value in server

13 posts, 0 answers
  1. Arvind
    Arvind avatar
    12 posts
    Member since:
    Jun 2012

    Posted 26 Jul 2012 Link to this post

    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
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 26 Jul 2012 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Peter
    Peter avatar
    98 posts
    Member since:
    Mar 2012

    Posted 22 Oct 2014 in reply to Jayesh Goyani Link to this post

    How do I do this in the UpdateCommand event?
  5. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 24 Oct 2014 in reply to Peter Link to this post

    Hi Peter,

    Can you please provide your code snippet?

    Thanks,
    Jayesh Goyani
  6. Mohammed BENKHDAR
    Mohammed BENKHDAR avatar
    7 posts
    Member since:
    May 2010

    Posted 10 Feb 2015 in reply to Jayesh Goyani Link to this post

    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
                                {
     
                                }
                            }
                    }

     
  7. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1798 posts

    Posted 13 Feb 2015 Link to this post

    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.

     
  8. Mohammed BENKHDAR
    Mohammed BENKHDAR avatar
    7 posts
    Member since:
    May 2010

    Posted 17 Feb 2015 in reply to Konstantin Dikov Link to this post

    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
  9. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1798 posts

    Posted 20 Feb 2015 Link to this post

    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.

     
  10. Mohammed BENKHDAR
    Mohammed BENKHDAR avatar
    7 posts
    Member since:
    May 2010

    Posted 20 Feb 2015 in reply to Konstantin Dikov Link to this post

    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"/>
                




  11. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1798 posts

    Posted 25 Feb 2015 Link to this post

    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.

     
  12. Mohammed BENKHDAR
    Mohammed BENKHDAR avatar
    7 posts
    Member since:
    May 2010

    Posted 19 May 2015 in reply to Konstantin Dikov Link to this post

    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.

  13. Jogender
    Jogender avatar
    1 posts
    Member since:
    Nov 2016

    Posted 08 Nov in reply to Jayesh Goyani Link to this post

    how to save multiple checkbox selected value in database with comma
  14. Eyup
    Admin
    Eyup avatar
    3012 posts

    Posted 11 Nov Link to this post

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