I have created a RadGrid control dynamically on Page_PreInit event and added it to a place holder on the page. my RadGridBuilder class has a Build() method.
My RadGrid has a custom button which is added at the time of ItemCreated event which is called during building the RadGrid (Page_PreInit):
protected virtual void RdGridItemCreated(object sender, GridItemEventArgs e)
{
switch (e.Item.ItemType)
{
// other codes
case GridItemType.CommandItem:
{
var gridCommandItem = e.Item as GridCommandItem;
AddPdfButton(gridCommandItem);
break;
}
}
}
private void AddPdfButton(GridCommandItem gridCommandItem)
{
var pdfButton = CreateExportToPdfButton();
try
{
PageUtil.RegisterPostBackControl(pdfButton);
// this is the cell which contains the export buttons.
((Table) gridCommandItem.Cells[0].Controls[0]).Rows[0].Cells[1].Controls.Add(pdfButton);
}
catch
{
// LOG the error silently
}
}
}
private Button CreateExportToPdfButton()
{
var result = new Button();
result.ID = "btnExportToPdf";
result.Click += ExportToPdfButtonClick;
result.CssClass = "rgExpPDF";
result.CommandName = "ExportToPdfCustomCommand";
result.Attributes["title"] = "Export to Pdf";
return result;
}
private void ExportToPdfButtonClick(object sender, EventArgs e)
{
// custom code
}
The Pdf icon appears as expected on the RadGrid. When it is clicked, the post back happens (and the RadGrid is regenerated obviously on Page_PreInit again), however, the ExportToPdfButtonClick method is never called.
Why it is not called? how to fix it? it may be related to viewstate and control state?
Thanks