Geeks With Blogs

@azamsharp
  • azamsharp The WWDC app says that there are new videos but there is nothing under videos!!! about 447 days ago
  • azamsharp Can I post my opinion on iOS 7 Beta or is it under NDA or something? about 447 days ago
  • azamsharp iOS 7 BETA installed successfully! about 447 days ago
  • azamsharp iOS 7 BETA installed! Now restoring! I am on NET10 which uses AT&T network. Hopefully 3G will still work! Lets c.. about 447 days ago
  • azamsharp The missing of button borders in Xcode 5 makes it hard to determine the clickable area. about 447 days ago
  • azamsharp I might wait for iOS to be released before I can put it on my device! Don't want a bricked or unstable device! about 447 days ago
  • azamsharp Android has many features that no one uses, number of versions that no one upgrades to and thousands of apps that no one pays for. about 447 days ago
  • azamsharp Maybe it is just me but the new iOS 7 UI looks like Windows Phone 8 UI! #maybeIamCrazy about 447 days ago
  • azamsharp Anyone using H20 network upgraded to iOS 7 BETA Keep me updated if you face any problems. about 447 days ago
  • azamsharp @merowing_ You already downloaded it! I cannot even load the developer's website! :( about 448 days ago

AzamSharp Some day I will know everything. I hope that day never comes.

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!

Posted on Saturday, September 9, 2006 8:31 PM | Back to top


Comments on this post: GridView Export to Excel Button Custom Control

# re: GridView Export to Excel Button Custom Control
Requesting Gravatar...
Good Utility
Left by vikram on Sep 10, 2006 12:10 AM

# re: GridView Export to Excel Button Custom Control
Requesting Gravatar...
It works pretty well out of the box, but I had to disable event validation on my page: enableEventValidation="false" since this code didn't work:

public override void VerifyRenderingInServerForm(Control control) {

}

Iván
Left by Iván on Sep 12, 2006 5:44 AM

# re: GridView Export to Excel Button Custom Control
Requesting Gravatar...
Ebrennan@hotmail.com

how do I go about registering the control on my system and then on the server. Do I place the DLL in a special folder.
Left by Eric Brennan on Nov 22, 2006 3:02 PM

# re: GridView Export to Excel Button Custom Control
Requesting Gravatar...
Hi,

I get an error as below. Can you help? Thanks!

Server Error in '/townsvillegps' Application.
--------------------------------------------------------------------------------

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[HttpException (0x80004005): Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.]
System.Web.UI.Page.VerifyRenderingInServerForm(Control control) +2045183
System.Web.UI.WebControls.GridView.Render(HtmlTextWriter writer, Boolean renderPanel) +37
System.Web.UI.WebControls.GridView.Render(HtmlTextWriter writer) +30
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
GridViewGuy.ExportToExcelButton.Export(GridView gv) +142
GridViewGuy.ExportToExcelButton.Pre_Export() +61
GridViewGuy.ExportToExcelButton.Btn_ExportClick(Object sender, EventArgs e) +5
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832
Left by Mike Edwards on Jul 17, 2007 12:07 AM

# re: GridView Export to Excel Button Custom Control
Requesting Gravatar...
Hi, I need source code that dll. how to do that. can you give me
Left by dhanabalan on Dec 20, 2007 3:52 AM

# re: GridView Export to Excel Button Custom Control
Requesting Gravatar...
it is very good
Left by sweta on Jan 10, 2008 10:21 PM

# re: GridView Export to Excel Button Custom Control
Requesting Gravatar...
It help me to solve my error!
Left by SPatel on Jan 23, 2008 2:42 AM

# re: GridView Export to Excel Button Custom Control
Requesting Gravatar...
Please Note that ViewState should be enabled on that very datagrid..
Thank you
Left by Assil Abdulrahim on Feb 11, 2008 5:25 PM

# re: GridView Export to Excel Button Custom Control
Requesting Gravatar...
I get the same error message as the other guy. What is the problem? I am new to this so I am sure it is something simple.
Left by Alan on Feb 22, 2008 3:55 PM

# re: GridView Export to Excel Button Custom Control
Requesting Gravatar...
May I suggest you to try http://www.gridviewtoexcel.com ?
Left by Ettore on Feb 29, 2008 11:25 PM

# re: GridView Export to Excel Button Custom Control
Requesting Gravatar...
Can u pls post the code behind the .dll file? the CS file?
Left by LostInDotNEt on Apr 18, 2008 1:06 AM

# re: GridView Export to Excel Button Custom Control
Requesting Gravatar...
Value cannot be null.
Parameter name: GridViewID Invalid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: GridViewID Invalid.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[ArgumentNullException: Value cannot be null.
Parameter name: GridViewID Invalid.]
GridViewGuy.ExportToExcelButton.Pre_Export() +135
GridViewGuy.ExportToExcelButton.Btn_ExportClick(Object sender, EventArgs e) +34
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3615; ASP.NET Version:2.0.50727.3053


I am getting error as above using the dll and code as described even giving the GridViewID correctly as that of my GridView????
Left by Kanika on Aug 25, 2010 8:40 PM

Your comment:
 (will show your gravatar)
 


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net | Join free