0

I am trying to use jquery for exporting grid view to excel. But I am not successful with the code. code referred from : http://www.c-sharpcorner.com/blogs/export-to-excel-using-jquery1 My code:

<script type="text/javascript">
        $("[id$=btnExcel]").click(function (e) {
            alert("testing");
            window.open('data:application/vnd.ms-   excel,' + encodeURIComponent($('#grdCharges').html()));  
            e.preventDefault();
        });
    </script>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div id="div_report" runat="server" visible="false">
                <div id="grdCharges" runat="server" style="width: 90%; overflow: auto;">
                    <asp:GridView ID="GridView1"
                        runat="server"
                        CellPadding="3"
                        CellSpacing="2"
                        AutoGenerateColumns="true"
                        ShowFooter="true"
                        FooterStyle-HorizontalAlign="Left"
                        RowStyle-BorderColor="Black" HeaderStyle-BackColor="#0CA3D2">
                        <FooterStyle BackColor="#87CEFA" />
                    </asp:GridView>
                </div>
            </div>            
        </ContentTemplate>        
    </asp:UpdatePanel>
    <div class="div_labels">
<asp:Button ID="btnExcel" class="input-submit" ClientIDMode="Static" runat="server" Text="Excel"  />               
            </div>  

But when I press the btnExcel I could not get even the alert window. Why is it so?

user4221591
  • 2,084
  • 7
  • 34
  • 68
  • try to move e.preventDefault(); b4 alert? if still not working, it might due to the updatepanel, as updatepanel will render jquery to not working, refer to this https://stackoverflow.com/questions/256195/jquery-document-ready-and-updatepanels – Se0ng11 Jul 11 '17 at 06:02
  • Start using a specialized library for creating Excel files, like [EPPlus](http://stackoverflow.com/documentation/epplus) for example. All you are doing now is creating a HTML page with an .xls extension. – VDWWD Jul 11 '17 at 06:40
  • P.S. the section where you have placed shared script. If its in `` section then removing it and adding them in `` tag would surely solve this issue. – Suprabhat Biswal Jul 11 '17 at 08:08

0 Answers0