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

Export to excel from RadToolbarButton

5 Answers 238 Views
Grid
This is a migrated thread and some comments may be shown as answers.
asimptota
Top achievements
Rank 1
asimptota asked on 21 Jan 2013, 11:06 PM
I'm using RadToolbar as CommandItem, and I want to be able to export grid data to Excel by pressing a button on toolbar. I have to mention that grid is ajaxified using RadAjaxManager. I'm having troubles disabling ajax on toolbar button
function onRequestStart(ajaxPanel, eventArgs) {
    if (eventArgs.EventTarget == "ExportToExcel") {
        eventArgs.EnableAjax = false;
    }
as I can't get the button in the toolbar. Can anyone, please, post some working sample code with exporting grid data from toolbar button?

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 22 Jan 2013, 04:36 AM
Hi,

Here is the full code that I tried based on your scenario.
aspx:
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" ClientEvents-OnRequestStart="onRequestStart">
           <AjaxSettings>
               <telerik:AjaxSetting AjaxControlID="RadGrid1">
                   <UpdatedControls>
                       <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                   </UpdatedControls>
               </telerik:AjaxSetting>
           </AjaxSettings>
       </telerik:RadAjaxManager>
       <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource2" AutoGenerateColumns="false">
           <MasterTableView CommandItemDisplay="Top">
               <CommandItemTemplate>
                   <telerik:RadToolBar ID="RadToolBar1" runat="server" OnButtonClick="RadToolBar1_ButtonClick">
                       <Items>
                           <telerik:RadToolBarButton Text="export">
                           </telerik:RadToolBarButton>
                       </Items>
                   </telerik:RadToolBar>
               </CommandItemTemplate>
               <Columns>
                   <telerik:GridBoundColumn DataField="OrderID" UniqueName="OrderID">
                   </telerik:GridBoundColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
C#:
protected void RadToolBar1_ButtonClick(object sender, Telerik.Web.UI.RadToolBarEventArgs e)
  {
      RadGrid1.MasterTableView.ExportToExcel();
  }
JS:
<script type="text/javascript">
    function onRequestStart(sender, args) {
        if (args.get_eventTarget().indexOf("RadToolBar1") >= 0)
            args.set_enableAjax(false);
    }
</script>

Thanks,
Shinu
0
asimptota
Top achievements
Rank 1
answered on 22 Jan 2013, 12:08 PM
Thank you so much. Export to excel now works. But, besides export button, I have few more buttons in my toolbar. For instance, refresh grid button. That button now weirdly renderes my grid. I guess it's because of the following line of code
if (args.get_eventTarget().indexOf("RadToolBar1") >= 0)
   args.set_enableAjax(false);
Is it because every button in toolbar now disables ajax? How to disable ajax only on export button?
0
Shinu
Top achievements
Rank 2
answered on 23 Jan 2013, 04:50 AM
Hi,

Try setting the following.
aspx:
<ExportSettings ExportOnlyData="true"></ExportSettings>

Thanks,
Shinu
0
asimptota
Top achievements
Rank 1
answered on 23 Jan 2013, 08:09 AM
No. it's not working. How can I disable ajax only on export button?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Jan 2013, 10:44 AM
Hello,

<CommandItemTemplate>
                    <telerik:RadToolBar ID="RadToolBar1" runat="server" AutoPostBack="true" OnClientButtonClicked="ClientButtonClicked">
                        <Items>
                            <telerik:RadToolBarButton Text="Edit selected" CommandName="ExportToExcel" CommandArgument="ExportToExcel">
                            </telerik:RadToolBarButton>
                            <telerik:RadToolBarButton Text="Update" CommandName="jayesh" CommandArgument="jayesh">
                            </telerik:RadToolBarButton>
                        </Items>
                    </telerik:RadToolBar>
                </CommandItemTemplate>
var IsExport = false;
 function onRequestStart(sender, args) {
 
     if (IsExport == true) {
         IsExport = false;
         args.set_enableAjax(false);
          
     }
 }
 
 function ClientButtonClicked(sender, args) {
 
     if (args.get_item().get_text() == "ExportToExcel") {
         IsExport = true;
     }
 }


Thanks,
Jayesh Goyani
Alvin
Top achievements
Rank 1
commented on 16 May 2022, 05:59 PM

Hi Jayesh,

I got the same question, and happened to see this post. I found the get_eventArgument() method at onRequestStart event at the documentation.

However, this method returns the sequence of the RadToolBarButtons. In the provided sample, it returns 1 instead of the CommandArgument. Is there a way to get the commend argument from the args in the onRequestStart event?

Thanks,

Alvin

Vessy
Telerik team
commented on 18 May 2022, 05:20 PM

Hi Alvin,

Can you share some more details on the scenario in which you are facing this behavior? Can you send me the exact setup leading to the issue so I can reproduce and examine it further at my end?

Alvin
Top achievements
Rank 1
commented on 18 May 2022, 06:05 PM

Hi,

What I am trying to do is identify the Export Button in the RadToolBar at onRequestStart event using get_eventArgument().

aspx:

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" ClientEvents-OnRequestStart="onRequestStart">
  <AjaxSettings>
         <telerik:AjaxSetting AjaxControlID="RadGrid1">
              <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
              </UpdatedControls>
          </telerik:AjaxSetting>
  </AjaxSettings>
</telerik:RadAjaxManager>
       <telerik:RadGrid ID="RadGrid1" runat="server"  AutoGenerateColumns="false">
           <MasterTableView CommandItemDisplay="Top">
               <CommandItemTemplate>
                   <telerik:RadToolBar ID="RadToolBar1" runat="server">
                       <Items>
                         <telerik:RadToolBarButton Text="Add" CommandArgument="Add" ></telerik:RadToolBarButton>
                         <telerik:RadToolBarButton Text="Delete" CommandArgument="Delete" ></telerik:RadToolBarButton>
                         <telerik:RadToolBarButton Text="Export" CommandArgument="Export"></telerik:RadToolBarButton>                           
                       </Items>
                   </telerik:RadToolBar>
               </CommandItemTemplate>
               <Columns>
                   <telerik:GridBoundColumn DataField="OrderID" UniqueName="OrderID"></telerik:GridBoundColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>

 

JS:

<script type="text/javascript">
    function onRequestStart(sender, args) {
        if( args.get_eventArgument()=='3') {     // What I want is 'Export', but it returns sequence of the RadToolBarbutton. 
             args.set_enableAjax(false);
         }            
    }
</script>

 

Thanks,

Alvin

Vessy
Telerik team
commented on 20 May 2022, 03:38 PM

Hi Alvin,

Thanks a lot for the provided sample.

The get_eventArgument() method of the AjaxManager gets the string passed in the initial AJAX request by the Toolbar that corresponds to the index of the clicked button and does not provide access to the commandArgument of the clicked button.

You can still access the CommandArgument of the clicked button in the Toolbar's OnClientClicked event like suggest by Jayesh above:

        <script>
            var IsExport = false;

            function onRequestStart(sender, args) {

                if (IsExport == true) {
                    IsExport = false;
                    args.set_enableAjax(false);
                }
            }

            function ClientButtonClicked(sender, args) {
                if (args.get_item().get_text() == "Export") {
                    IsExport = true;
                }
            }
        </script>

Tags
Grid
Asked by
asimptota
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
asimptota
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or