GridView Export to Excel Button Custom Control

Since, most of the applications these days require a feature to export the GridView to excel so I decided to make it into a custom control. The control was actually a simple user control which was converted to the custom control using the Visual Studio.NET 2005 Publish and ASP.NET build features. The control is in the form of the dll so you can simply download it and reference the assembly in your application. Once, you have the reference to the assembly you can simply use the control as following:

Register the Control to the Page:

<%@ Register Namespace="ASP" TagPrefix="ExportButton" Assembly="App_Web_exporttoexcelbutton.ascx.cdcab7d2" %>

After registering the control you can use it as follows:

<ExportButton:exporttoexcelbutton_ascx ID="MyExportButton" runat="server" GridViewID="GridView1" FileName="MyExcelFile.xls" />

As, you can see that the properties of the ExportButton are quite simple. The GridViewID denotes the ID of the GridView which you are trying to export and the FileName attribute denotes the name of the exported file. You can also use the ExportButton control with the master pages all you need to find is the correct ID of the GridView and you are good to go. Here is a simple example that shows the GridView which is contained inside the ContentPlaceHolder.

<ExportButton:ExportToExcelButton ID="ExportToExcelButton1" GridViewID="ctl00$ContentPlaceHolder1$GridView1" FileName="NewFile.xls" runat="server">

</ExportButton:ExportToExcelButton>

 

NOTE:

Before using the control you need to add the following lines in your Page code behind or the BasePage code behind.

public override void VerifyRenderingInServerForm(Control control)

{

}

If you have DropDownLists or LinkButtons inside the GridView control then they will be converted into the Literal controls.

You can download the control here (source code is also available with the download).

Hope it helps!

Print | posted @ Saturday, September 09, 2006 8:31 PM

Twitter