Export to excel Download popup window not working in asp.net

If you are using  asp:ScriptManager  and UpdatePanel to use ajax and Download Popup is not showing you then you have to use Trigger as you can see in below code.

 <asp:ScriptManager ID="scmSearch" runat="server">
        </asp:ScriptManager>

        <asp:UpdatePanel ID="up" runat="server">
         <Triggers>
            <asp:PostBackTrigger ControlID="btnExport" />
        </Triggers>

            <ContentTemplate>

 

private  void ExportGridView(DataGrid dg, string reportName)
    {

        Response.AddHeader("Content-Disposition", "attachment;filename=" + reportName + ".xls");
        Response.AddHeader("Status", "200 OK");
        Response.ContentEncoding = System.Text.Encoding.Unicode;
        Response.BinaryWrite(Encoding.Unicode.GetPreamble());
        Response.ContentType = "application/vnd.ms-excel";
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        dg.RenderControl(hw);
        Response.Write(RenderGridView(dg));
        Response.End();
    }
    public  string RenderGridView(DataGrid dg)
    {
        string returnvalue = string.Empty;
        try
        {
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter writer = new HtmlTextWriter(stringWrite);
             dg.RenderControl(writer);
            returnvalue = writer.InnerWriter.ToString();
        }
        catch (Exception ex)
        {

        }
        return returnvalue;
    }

Related Alrticles

FREE!!! Registration