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

Programmatically created columns

1 Answer 126 Views
Grid
This is a migrated thread and some comments may be shown as answers.
gershie
Top achievements
Rank 1
gershie asked on 01 Dec 2008, 08:00 PM
I would like to have template columns that get added programmatically but I have noticed differences in the behavior of columns defined in markup and columns added programmatically. When I click on buttons of columns that are defined in markup, their event handlers fire properly. When I cilck on buttons of columns added programmatically, the button doesn't exist anymore, the event isn't fired, and the column isn't rendered properly after the postback.

I have tried to put together an example where there are two columns in a grid that should be exactly the same except that one is added in markup and one is added in codebehind, and I've put in the NeedDataSource and ColumnCreating handlers to get paging working. To see what I mean click on a button in the first column then click on a button in the second column. Is there some way that I can get the programmatically-added column to maintain its state like the other column?

markup:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RadGridTest.aspx.cs" Inherits="KMI.IncidentWeb.RadGridTest" %> 
<%@ Register TagPrefix="Telerik" Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">  
    <head runat="server">  
        <title>Untitled Page</title> 
    </head> 
    <body> 
        <form id="form1" runat="server">  
            <div> 
                <asp:ScriptManager ID="MasterScriptManager" runat="server" /> 
 
<Telerik:RadGrid runat="server" ID="grid" AutoGenerateColumns="false" 
        AllowCustomPaging="true" AllowPaging="true" PageSize="3" VirtualItemCount="8">  
    <MasterTableView> 
        <Columns> 
            <Telerik:GridTemplateColumn> 
                <HeaderTemplate> 
                    <asp:Button runat="server" OnClick="button_Click" /> 
                </HeaderTemplate> 
                <ItemTemplate> 
                    <asp:Button runat="server" OnClick="button_Click" Text='<%# Eval("Name") %>' CommandArgument='<%# Eval("Id") %>' /> 
                </ItemTemplate> 
            </Telerik:GridTemplateColumn> 
        </Columns> 
    </MasterTableView> 
</Telerik:RadGrid> 
 
            </div> 
        </form> 
    </body> 
</html> 
 

codebehind:

 

using System;  
using System.Linq;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
 
namespace KMI.IncidentWeb  
{  
    /// <summary> 
    /// Simple template to match the template defined declaratively on the page  
    /// </summary> 
    public class Template : ITemplate  
    {  
        public void InstantiateIn(Control container)  
        {  
            // Create a control in the container that has its data binding event handled  
            PlaceHolder placeholderTemplateContent = new PlaceHolder();  
            placeholderTemplateContent.DataBinding += new EventHandler(placeholderTemplateContent_DataBinding);  
            container.Controls.Add(placeholderTemplateContent);  
        }  
 
        protected void placeholderTemplateContent_DataBinding(object sender, EventArgs e)  
        {  
            PlaceHolder placeholderTemplateContent = sender as PlaceHolder;  
 
            object dataItem = DataBinder.GetDataItem(placeholderTemplateContent.NamingContainer);  
 
            Button button = new Button();  
            if (dataItem != null)  
            {  
                button.Text = DataBinder.Eval(dataItem, "Name").ToString();  
                button.CommandArgument = DataBinder.Eval(dataItem, "Id").ToString();  
            }  
            button.Click += new EventHandler(button_Click);  
            placeholderTemplateContent.Controls.Add(button);  
        }  
 
        protected void button_Click(object sender, EventArgs e)  
        {  
            throw new NotImplementedException((sender as Button).CommandArgument);  
        }  
    }  
 
    /// <summary> 
    /// Simple column to match the column defined declaratively on the page  
    /// </summary> 
    public class Column : Telerik.Web.UI.GridTemplateColumn  
    {  
        public Column()  
        {  
            ItemTemplate = new Template();  
            HeaderTemplate = new Template();  
        }  
    }  
 
    public partial class RadGridTest : System.Web.UI.Page  
    {  
        protected override void OnInit(EventArgs e)  
        {  
            base.OnInit(e);  
            grid.NeedDataSource += new Telerik.Web.UI.GridNeedDataSourceEventHandler(grid_NeedDataSource);  
            grid.ColumnCreating += new Telerik.Web.UI.GridColumnCreatingEventHandler(grid_ColumnCreating);  
        }  
 
        protected void grid_ColumnCreating(object sender, Telerik.Web.UI.GridColumnCreatingEventArgs e)  
        {  
            e.Column = new Column();  
        }  
 
        protected void grid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
        {  
            // Mock datasource that has a column of integers and a column of texts  
            grid.DataSource = from int id in new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }  
                where id >= grid.PageSize * grid.CurrentPageIndex && id < grid.PageSize * (grid.CurrentPageIndex + 1)  
                select new { Id = idName = string.Format("The number {0}", id) };  
        }  
 
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!Page.IsPostBack)  
            {  
                grid.Columns.Add(new Column());  
                grid.Rebind();  
            }  
        }  
 
        protected void button_Click(object sender, EventArgs e)  
        {  
            throw new NotImplementedException("Event handled with argument: " + (sender as Button).CommandArgument);  
        }  
    }  
}  
 

Thanks in advance.

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 02 Dec 2008, 05:31 AM
Hi Gershie,

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.
Programmatic creation

Shinu
Tags
Grid
Asked by
gershie
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or