connectseekers.com

Thursday, 31 July 2014

GridView Export to HTML in Asp.Net Using C#.Net and VB.Net

7/31/2014 - By Pranav Singh 0

This article will show you how you can export the gridview control data into html data. In this we will see to get the records of gridview into html format. In this article I have used asp.net, gridview, c#.net, vb.net, button control and sql data base.

Now for this article first we will create a new asp.net application and add the below code into your aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HTMLExport.aspx.cs" Inherits="ProjectDemo_Asp.et.PDFExport" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GridView Export to HTML in Asp.Net Using C#.Net and VB.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            BorderStyle="Solid" EmptyDataText="There are no data records to display.">
            <Columns>
                <asp:BoundField DataField="author_name" HeaderText="NAME" />
                <asp:BoundField DataField="publisher_name" HeaderText="PUBLISHER NAME" />
                <asp:BoundField DataField="publication_year" HeaderText="PUBLISH YEAR" />
                <asp:BoundField DataField="retail_price" HeaderText="PRICE" />
            </Columns>
            <HeaderStyle BackColor="#66CCFF" />
        </asp:GridView>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Export To HTML" />
   
    </div>
    </form>
</body>
</html>

Now just check the below code to export the record.

C#.Net
using System;
using System.Web.UI;
using System.Data;
using System.Text;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
namespace ProjectDemo_Asp.et
{
    public partial class PDFExport : System.Web.UI.Page
    {
        public string connectionstring = "<-- connection="" string-="" your="">";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                /*Data to display on screen*/
                DataTable _objdt = new DataTable();
                _objdt = GetDataFromDataBase();
                if (_objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = _objdt;
                    GridView1.DataBind();
                }
            }
        }

        ///


        /// Function for binding retribing the data from database
         ///
        public DataTable GetDataFromDataBase()
        {
            DataTable _objdt = new DataTable();
            string querystring = "select * from Books;";
            SqlConnection _objcon = new SqlConnection(connectionstring);
            SqlDataAdapter _objda = new SqlDataAdapter(querystring, _objcon);
            _objcon.Open();
            _objda.Fill(_objdt);
            return _objdt;
        }
        ///


        /// Click event to export the grivview data into HTML format
        ///

        ///
        ///
        protected void Button1_Click(object sender, EventArgs e)
        {
            /*Data to bind to gridview and then export to HTML*/
            DataTable _objdt = new DataTable();
            _objdt = GetDataFromDataBase();
            if (_objdt.Rows.Count > 0)
            {
                GridView1.DataSource = _objdt;
                GridView1.DataBind();
            }
            HtmlForm _objform = new HtmlForm();
            Response.ClearContent();
            Response.AddHeader("content-disposition"string.Format("attachment; filename={0}""BookStore.html"));
            Response.Charset = "";
            Response.ContentType = "text/html";
            StringWriter _objstringbuilder = new StringWriter();
            HtmlTextWriter objhtmltextwriter = new HtmlTextWriter(_objstringbuilder);
            _objform.Attributes["runat"] = "server";
            _objform.Controls.Add(GridView1);
            this.Controls.Add(_objform);
            Form.RenderControl(objhtmltextwriter);
            Response.Write(_objstringbuilder.ToString());
            Response.Flush();
            Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }
    }
}

       

VB.Net
Imports System.Web.UI
Imports System.Data
Imports System.Text
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Data.SqlClient
Namespace ProjectDemo_Asp.et
    Partial Public Class PDFExport
        Inherits System.Web.UI.Page
        Public connectionstring As String = "<-- connection="" string-="" your="">"
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                'Data to display on screen

                Dim _objdt As New DataTable()
                _objdt = GetDataFromDataBase()
                If _objdt.Rows.Count > 0 Then
                    GridView1.DataSource = _objdt
                    GridView1.DataBind()
                End If
            End If
        End Sub

        '''


        ''' Function for binding retribing the data from database
        '''
        Public Function GetDataFromDataBase() As DataTable
            Dim _objdt As New DataTable()
            Dim querystring As String = "select * from Books;"
            Dim _objcon As New SqlConnection(connectionstring)
            Dim _objda As New SqlDataAdapter(querystring, _objcon)
            _objcon.Open()
            _objda.Fill(_objdt)
            Return _objdt
        End Function
        '''


        ''' Click event to export the grivview data into HTML format
        '''

        '''
        '''
        Protected Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs)
            'Data to bind to gridview and then export to HTML

            Dim _objdt As New DataTable()
            _objdt = GetDataFromDataBase()
            If _objdt.Rows.Count > 0 Then
                GridView1.DataSource = _objdt
                GridView1.DataBind()
            End If
            Dim _objform As New HtmlForm()
            Response.ClearContent()
            Response.AddHeader("content-disposition"String.Format("attachment; filename={0}""BookStore.html"))
            Response.Charset = ""
            Response.ContentType = "text/html"
            Dim _objstringbuilder As New StringWriter()
            Dim objhtmltextwriter As New HtmlTextWriter(_objstringbuilder)
            _objform.Attributes("runat") = "server"
            _objform.Controls.Add(GridView1)
            Me.Controls.Add(_objform)
            Form.RenderControl(objhtmltextwriter)
            Response.Write(_objstringbuilder.ToString())
            Response.Flush()
            Response.[End]()
        End Sub
        Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
            ' Verifies that the control is rendered
        End Sub
    End Class
End Namespace
        
 No we have done. Just check the detail of the code. In above code I have first bind the code and after that I have written code to export the data.

OUTPUT


Now click on export button.


Now click to open the file.


DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top