Microsoft has done a wonderful job of creating an extensible framework, especially for ASP.NET, by creating various server controls. This article is about extending ASP.NET
Button control and adding your own functionality for ONE CLICK exporting from ASP.NET page.
There are various ways one can export data using ASP.NET. Usually, it is done by coding a separate page and adding various HTTP headers and responses. (Refer: Microsoft).
Like me, most of us believe in reuse. The export functionality can be achieved by reusing the same page for various projects either by passing
DataSet from the parent page, or reconnecting to the data source on the landing page and manipulating
One Click Export
You may wonder why I call it ONE CLICK EXPORT, 'because this control doesn't require intermediate page for exporting data'. Just drag the
ExportButton control on an ASPX page or a user control, and set its properties at design time or runtime to hook
DataView. Yup, that’s it, no more hassles of dealing to investigate HTTP headers, MIME types or encodings.
<pnwc:ExportButton id="btnExcel" runat="server"
Separator="TAB" Text="Export to Excel"
'Code Behind [Run time]
Private Sub Page_Load(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds as dataset=filldataset()
dgBooks.DataSource = ds.Tables("Books")
'Set Export button properties
btnExcel.Dataview = ds.Tables("Books").DefaultView
btnExcel.FileNameToExport = "Books.xls"
btnExcel.ExportType = _
btnExcel.Separator = _
ExportButton properties can either be set at design time and/or run time].
ExportType [CSV or EXCEL]
Separator [Comma or TAB]
Delimiter [Any character]
ExportType property is used to either select CSV or EXCEL format,
btnExcel.ExportType = PNayak.Web.UI.WebControls.ExportButton.ExportTypeEnum.Excel.
Separator property can be used to either select TAB or Comma separator,
btnExcel.Separator = PNayak.Web.UI.WebControls.ExportButton.SeparatorTypeEnum.TAB.
Delimiter property is used to specify any delimiter like single quote, double quote, pipe character etc. Default is
Nothing (empty string).
The source code is provided with the sample project to test out the solution.
- Install is straightforward. Extract demo code zip files to a designated directory.
- Two directories will be created. (namely, \\PNWebControls and \\TestMyControls). Make sure they both fall under same parent directory.
- Create a virtual directory by either right clicking on the directory \\TestMyControls and opening properties for web share, or use IIS MMC. (Note: This is a demo project.)
- Open up a demo solution (TestPNControls.sln) in Visual Studio 2003. (This control also works well with VS 2002. You know the drill, just change the version numbers inside the project/solution file and open it in Visual Studio 2002).
How to use the Control in your project
You may either include the PNWebControls project and reference it in your project, or reference the assembly (PNayak.Web.UI.WebControls.ExportButton.dll). You can also add this control in Visual Studio Toolbox for design time.
[Note: This assembly also contains
ExportLinkButton as some may prefer link button over regular button.]
Check out my .NET spot for more to discover.
- 8 July 2004 - updated downloads.
- 9 Dec 2004 - updated downloads and article.