Search code examples
c#javascriptdevexpressexport-to-excelribbon

DevExpress Export ASPxGridView when a ribbon button item is clicked


I am trying to export my grid view when I click on a ribbon button

The problem is tha the ribbon button click is a client side event and the export function is handled by the server.

    <script type="text/javascript">
    //Ribbon events
    function onCommandExecuted(s, e) {
        var name = e.item.name;
        if (name == "Ajouter") {
            grid.AddNewRow();
        }

        else if (name == "Modifier") {
            grid.StartEditRow(grid.GetFocusedRowIndex());
        }
        else if (name == "Supprimer") {
            if (confirm("Vous confirmez la suppression de l'élement selectionné ?")) {
                grid.DeleteRow(grid.GetFocusedRowIndex());
            }
        }
        else if (name = "Excel")
        {
           //What to do here?
        }
    }


</script>
  //The export function (C#)
  protected void btnXlsExport_Click(object sender, EventArgs e)
{
    gridExport.WriteXlsToResponse();
}

How do I call the export function in my script.Thanks.


Solution

  • As was mentioned before you can export GridView only from server site after postback. You can add near your gridview hidden control, something like from code below

    <dx:ASPxButton runat="server" ID="_exportBtn" ClientInstanceName="_exportBtn" ClientVisible="false" OnClick="OnClickExport" />
    

    move the unloading code to the OnClickExport method

        public void OnClickExport(object sender, EventArgs args)
        {
            gridExport.WriteXlsxToResponse();
        }
    

    and then click on it from you client side code

        else if (name = "Excel")
        {
           //What to do here?
           _exportBtn.OnClick();
        }