Friday, 4 March 2016

Export Gridview to Excel Inside of Ajax UpdatePanel Using in Asp.Net C#

Export Grid view to Excel Inside of Ajax 

Update Panel 


Grid view Data Export to Excel inside of ajax update panel using in asp.net c#.

                                  DEMO

                                              

                             Download
                      
                       HTML CODING

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Export Gridview to Excel Inside of Ajax UpdatePanel Using in Asp.Net C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"><ContentTemplate>
            <asp:Gridview ID="GridView1" runat="server" AutoGenerateColumns="false">
                <AlternatingRowStyle ForeColor="Gray" />
               <Columns>
                  <asp:BoundField  HeaderText="Employee Name" DataField="Empname"/> <asp:BoundField  DataField="Id" HeaderText="Employee Id"/>
               </Columns>
                <HeaderStyle ForeColor="Blue" />
            </asp:Gridview>

                                                         
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button ID="btnExportExcel" runat="server" OnClick="btnExportExcel_Click" Text="Export to Excel" />

                                                          </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="GridView1" />
                <asp:PostBackTrigger ControlID="btnExportExcel" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>



                               C# CODING


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class Export_Gridview : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        if (!IsPostBack)
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from Employee",con);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
    protected void btnExportExcel_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition""attachment; filename=CustomerDetails.xls");
        Response.ContentType = "application/excel";
        StringWriter SW = new StringWriter();
        HtmlTextWriter HTW = new HtmlTextWriter(SW);
        GridView1.RenderControl(HTW);
        Response.Write(SW.ToString());
        Response.End();

    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }


}






















0 comments:

Post a Comment