Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
65
TemplateDataField postback
posted

Hi,

     I have a WebDataGrid with a programmatically added template column, which contains a Button; everything works fine, except that the ItemCommand event does not fire when the user clicks the button. 

Here is the code (I am using the webdatagrid in a CompositeContro, and setting DataSource and calling DataBind from the pagel):

	public class MyGrid : CompositeControl
	{
		private WebDataGrid _grid = new WebDataGrid();
 
		public MyGrid()
		{
			_grid.AutoGenerateColumns = false;
 			_grid.Columns.Add(new TemplateDataField()
			                  	{
									Key = "MyButtonTemplate",
									Width = Unit.Pixel(100)
			                  	});
 
			_grid.Columns[0].Header.Text = "my template";
 
			ItemTemplate itemTemplate1 = new ItemTemplate();
			itemTemplate1.Template = new ButtonTemplate();
			_grid.Templates.Add(itemTemplate1);
 
			((ItemTemplate)_grid.Templates[_grid.Templates.Count - 1]).TemplateID = "myTemplate";
 
			EnsureChildControls();
		}
 
		protected override void CreateChildControls()
		{
			base.CreateChildControls();
 			Controls.Add(_grid);
		}
 
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
 
			_grid.ItemCommand += _grid_ItemCommand;
		}
 
		void _grid_ItemCommand(object senderHandleCommandEventArgs e)
		{
			// not firing
		}
 
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
 
			ItemTemplate itemTemplate = (ItemTemplate)_grid.Templates[0];
			for (int i = 0i < _grid.Rows.Counti++)
			{
				_grid.Rows[i].Items.FindItemByKey("MyButtonTemplate").Template = itemTemplate.Template;
			}
		}
 
		public DataTable DataSource
		{
			get
			{
				return _grid.DataSource as DataTable;
			}
			set
			{
				_grid.DataSource = value;
			}
		}
	}

 

    The event fires when I create the template column directly in the grid definition (aspx).

    How can I solve my problem?

Thanks in advance,

      Giovanni

Parents
No Data
Reply
  • 37874
    posted

    Hi zangrossi,

     

    Thank you for posting in the community.

     

    I have been looking into the behavior that you have described and I can suggest you to set the ItemTemplate using the following syntax:

     

    _grid.Columns.Add(new TemplateDataField()

    {

        Key = "MyButtonTemplate",

        Width = Unit.Pixel(100),

        ItemTemplate = new CustomItemTemplate()

                   

    });

     

    And create the CustomItemTemplate class – for example:

     

        public class CustomItemTemplate : ITemplate

        {

            #region ITemplate Members

     

            public void InstantiateIn(Control container)

            {

     

                LinkButton edit = new LinkButton();

                edit.CssClass = "LinkButton";

                edit.Text = "Edit";

                edit.OnClientClick = "return editRow()";

                container.Controls.Add(edit);

            }

     

            #endregion

        }

     

    For more information about dynamically adding ItemTemplates check the following link - http://help.infragistics.com/Help/NetAdvantage/ASPNET/2011.2/CLR4.0/html/WebDataGrid_Using_Item_Template.html. I tested your code with these modifications and the ItemCommand fired.

     

    Please let me know if this helps.

     

Children