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

When/where should a custom command button be added to the RadGrid?

1 Answer 324 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pooya
Top achievements
Rank 1
Pooya asked on 19 Aug 2011, 11:12 AM
I have created a RadGrid dynamically and added it to a placeholder at Page_Init event.

I'd need to add a custom button to the CommandItem and for this I've tried many scenarios and none of which has worked properly:

1) I added the custom button at RadGrid_ItemCreated event. The button appears. When it's clicked, it posts back, however, its click event handler is never raised. why? how to fix it?

My button is a normal ASP.NET button:

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;
        }

Also I tried managing it in the ItemCommand event when the button is clicked: 

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)       
{
    if(e.CommandName == "ExportToPdfCustomCommand")
    {
        //my code here
    }
}

but ItemCommand event doesn't get raised either when the button is clicked.

2) I added the custom button at RadGrid_MasterTableView_Load. The button appears and when it's clicked, it posts back and click event is raised as expected which is great. As soon as sorting is enabled on the RadGrid and a column header is selected to be sorted, the sorting happens however my custom button disappears. Why? how to fix it?

3) On RadGrid_MasterTableView_Init; behaves similar to #2

4) On RadGrid_MasterTableView_PreRender; behaves similar to #1

5) On RadGrid_Init, behaves similar to #2

6) Another possible approach comes to mind is to create a new CommandItemTemplate: http://www.telerik.com/help/aspnet/grid/grdcommanditemtemplate.html

in which case, the click event and itemcommand events are raised properly.

But there is an issue with this approach that it would be more complex as I'm using the built-in Export to Word and Excel functionalities of RadGrid currently. so not sure how to add these 2 to the CommandItemTemplate this way?

I'd rather making the previous approaches working if possible.

Your help is very much appreciated on any of these approaches.

1 Answer, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 24 Aug 2011, 09:46 AM
Hello Pooya,

Here is an example how to place button in the command item of ajaxified grid that to cause export to excel.

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
  <script type="text/javascript">
    function RequestStart(sender, eventArgs)
    {
      var eventTarget = eventArgs.get_eventTarget();
      if (eventTarget.indexOf("ExportToExcelButton") != -1)
      {
        eventArgs.set_enableAjax(false);
      }
    }
  </script>
</telerik:RadCodeBlock>
<div>
  <telerik:RadAjaxPanel runat="server" ClientEvents-OnRequestStart="RequestStart">
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowSorting="true" OnNeedDataSource="RadGrid1_NeedDataSource">
      <MasterTableView CommandItemDisplay="Top">
        <CommandItemTemplate>
          <asp:Button ID="ExportToExcelButton" Text="Export to excel" runat="server" CommandName=" <%#RadGrid.ExportToExcelCommandName %>" />
        </CommandItemTemplate>
      </MasterTableView>
    </telerik:RadGrid>
  </telerik:RadAjaxPanel>

Additionally you could see this help topic: http://www.telerik.com/help/aspnet/grid/grdexportwithajax.html

I hope this helps.

Kind regards,
Vasil
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Grid
Asked by
Pooya
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Share this question
or