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 sender, HandleCommandEventArgs e) { // not firing } protected override void OnLoad(EventArgs e) { base.OnLoad(e); ItemTemplate itemTemplate = (ItemTemplate)_grid.Templates[0]; for (int i = 0; i < _grid.Rows.Count; i++) { _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
Hello zangrossi,
I'm just checking if you have resolved your issue.
Hi zangrossi,
You could try the following approach:
edit.OnClientClick = "editRow()";
And then in editRow function get the grid using this line:
var grid = $find("<%= WebDataGrid.ClientID %>");
Let me know if this helps.
Ok, I've called the editRow() on the correct javascript object. My onClientClick property is now set with:
"return document.getElementById('###internalGridId###').editRow();"
where ###internalGridId### is the webdatagrid clientId (I am wrapping the webdatagrid in another control). Now the problem is that the ' character is rendered as " and the js code throws an exception... I've tried using \', \", anything... any ideas?
Hi Nikolay,
I tried your code, but I have a javascript error on the editRow() function call....
Feel free to contact me if you have any questions.