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

How to get Checkbox control added dynamically in GridTemplate Column?

4 Answers 591 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kaushal
Top achievements
Rank 1
Kaushal asked on 15 Jul 2011, 10:56 AM
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

 

 

 


4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 15 Jul 2011, 11:46 AM
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
0
Princy
Top achievements
Rank 2
answered on 15 Jul 2011, 11:52 AM
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.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 26 Jul 2011, 05:45 PM
0
Jaya
Top achievements
Rank 1
answered on 21 Feb 2015, 08:25 AM
hi

Can u solve this

http://www.telerik.com/forums/how-to-use-findcontrol-in-telerik-radgrid
Tags
Grid
Asked by
Kaushal
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Jaya
Top achievements
Rank 1
Share this question
or