How to get Checkbox control added dynamically in GridTemplate Column?

5 posts, 0 answers
  1. Kaushal
    Kaushal avatar
    47 posts
    Member since:
    Sep 2010

    Posted 15 Jul 2011 Link to this post

    Hi,

    Here I have one radgrid in which I have two boundcolumns and others are I have added dynamically at runtime "GridTemplateColumn" from code behind. In which I have added one check box control in ItemTemplate. Here also I have one save button outside of the radgrid. Now when I am clicking on that save button I can not getting that checkbox added dynamically in columns on click of save button. please assist me for the same.

    Adding columns like:

    GridTemplateColumn templateColumn = new GridTemplateColumn();

    templateColumn.ItemTemplate = new MyTemplate(obj.Name.ToString());

    templateColumn.DataField = obj.ActionMenuID.ToString();

    templateColumn.HeaderText = obj.Name;

    templateColumn.UniqueName = obj.Name;

    radgrid1.MasterTableView.Columns.Add(templateColumn);

    class MyTemplate : ITemplate  <- Class called at the time of above column creation to generate checkbox under the itemtemplate

     

     

    public void InstantiateIn(System.Web.UI.Control container)
    {
            checkBox = new CheckBox();
            checkBox.ID = colname;
            container.Controls.Add(checkBox);
        }

     

    Now how to get this checkbox on click of save button?

    Thanks you,

    Regards,

    Kaushal Jani

     

     

     


  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 15 Jul 2011 Link to this post

    hi kaushal Jani,

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
            <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false"
                onneeddatasource="RadGrid1_NeedDataSource" >
                <MasterTableView DataKeyNames="ID">
                    <Columns>
                        <telerik:GridBoundColumn DataField="ID" HeaderText="ID"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Name" HeaderText="Name"></telerik:GridBoundColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>
            <br />
            <telerik:RadButton ID="RadButton1" Text="Click for selected item" runat="server" onclick="RadButton1_Click"></telerik:RadButton>
            <br />
            <asp:Label ID="lblTest" runat="server"></asp:Label>
        </div>
        </form>
    </body>
    </html>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
    using System.Data;
     
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            
            GridTemplateColumn test1 = new GridTemplateColumn();
            test1.HeaderText = "Check Box";
            test1.UniqueName = "CheckBoxColumn";
           
            test1.ItemTemplate = new MyTemplate("test");
     
            GridTemplateColumn test2 = new GridTemplateColumn();
            test2.HeaderText = "Check Box";
            test2.UniqueName = "CheckBoxColumn2";
           
            test2.ItemTemplate = new MyTemplate("test2");
     
            RadGrid1.MasterTableView.Columns.Add(test1);
            RadGrid1.MasterTableView.Columns.Add(test2);
             
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
                    new { ID = 1, Name ="Name1"},
                    new { ID = 2, Name = "Name2"},
                    new { ID = 3, Name = "Name3"},
                    new { ID = 4, Name = "Name4"},
                    new { ID = 5, Name = "Name5"},
                    new { ID = 6, Name ="Name6"},
                    new { ID = 7, Name = "Name7"},
                    new { ID = 8, Name = "Name8"},
                    new { ID = 9, Name = "Name9"},
                    new { ID = 10, Name = "Name10"},
                    new { ID = 11, Name ="Name11"},
                    new { ID = 12, Name = "Name12"},
                    new { ID = 13, Name = "Name13"},
                    new { ID = 14, Name = "Name14"},
                    new { ID = 15, Name = "Name15"}
                };
     
            RadGrid1.DataSource = data;
        }
        protected void RadButton1_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in RadGrid1.Items)
            {
                 
                CheckBox chkTest = (CheckBox)item["CheckBoxColumn"].Controls[0];
                if (chkTest.Checked)
                {
                    lblTest.Text += "_"+ item.GetDataKeyValue("ID").ToString();
                }
     
                CheckBox chkTest2 = (CheckBox)item["CheckBoxColumn2"].Controls[0];
                if (chkTest2.Checked)
                {
                    lblTest.Text += ";" + item.GetDataKeyValue("ID").ToString();
                }
            }
        }
    }
     
    public class MyTemplate : ITemplate
    {
         
        protected CheckBox boolValue;
        private string colname;
     
        public MyTemplate(string cName)
        {
            colname = cName;
        }
     
        public void InstantiateIn(System.Web.UI.Control container)
        {
            boolValue = new CheckBox();
            boolValue.ID = colname;
            container.Controls.Add(boolValue);
        }
        void boolValue_DataBinding(object sender, EventArgs e)
        {
            CheckBox cBox = (CheckBox)sender;
            GridDataItem container = (GridDataItem)cBox.NamingContainer;
        }
         
    }

    u can also access this combo
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
                if (Convert.ToInt32(item.GetDataKeyValue("ID")) == 1)
                {
                    CheckBox chkTest = (CheckBox)item["CheckBoxColumn"].Controls[0];
                    chkTest.Checked = true;
                }
                if (Convert.ToInt32(item.GetDataKeyValue("ID")) == 3)
                {
                    CheckBox chkTest2 = (CheckBox)item["CheckBoxColumn2"].Controls[0];
                    chkTest2.Checked = true;
                }
            }
        }


    let me if any concern.

    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 15 Jul 2011 Link to this post

    Hello Kaushal,

    RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file. Check the following help documentation which explains this.
    Programmatic Creation.

    Thanks,
    Princy.
  5. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 26 Jul 2011 Link to this post

  6. Jaya
    Jaya avatar
    81 posts
    Member since:
    Mar 2011

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

    hi

    Can u solve this

    http://www.telerik.com/forums/how-to-use-findcontrol-in-telerik-radgrid
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017