1

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

The Light
  • 26,341
  • 62
  • 176
  • 258

2 Answers2

2

Try binding this event on page_init or page_load as per

ASP.Net: why is my button's click/command events not binding/firing in a repeater?

http://forums.asp.net/t/1129248.aspx/1/10

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • no, I have tested it with page_load, pare_init and page_preinit; the event handler is not raised. page_load is raised after the view state has been loaded. – The Light Aug 19 '11 at 09:21
  • are you sure your code is correct.. as this is how you bind dynamic handlers. What did you add to page_init? – Adam Tuliper Aug 19 '11 at 13:49
  • 1
    yeah the code is correct; RadGrid seems to require its own way of plumbing! - see my answer. – The Light Aug 20 '11 at 07:56
0

RadGrid seems to be different apparently and didn't figure out why the event wasn't raised but found a solution.

I created a new class implementing ITemplate interface and assigning it to the CommandItemTemplate property of my RadGrid.MasterTableView object.

Then adding my custom controls in the Instantiate() method of my new class and finally the click event is raised; basically rebuilding my Command Row:

http://www.telerik.com/help/aspnet-ajax/grid-commanditemtemplate.html

Not sure at what point these controls are added to the RadGrid but must be before the RadGrid_ItemCreated and RadGrid_Load events and after RadGrid_Init event. anyway, it's resolved now.

The Light
  • 26,341
  • 62
  • 176
  • 258