Link to home
Start Free TrialLog in
Avatar of Sujinig
Sujinig

asked on

Export to excel data from datagrids in asp.net that has stylesheet.

I have a asp.net page where when user selects certain filters, i show them the result data in multiple datagrids. I need to export these data to a excel sheet when they click a button.

the code i used is as follows
 
                                                Response.Clear ();
                  Response.Charset = "";
                  Response.ContentType = "application/vnd.ms-excel";
                  
                  System.IO.StringWriter stringwriter = new System.IO.StringWriter ();
                  System.Web.UI.HtmlTextWriter htmltextwriter = new HtmlTextWriter (stringwriter);

                  
                  dgMoneyContributions.GridLines = GridLines.None ;
                  dgProductContributions.GridLines = GridLines.None ;
                  dgStockContributions.GridLines = GridLines.None ;
                  dgThriftContributions.GridLines = GridLines.None ;
                  dgInKindContributions.GridLines = GridLines.None ;

                  BindData (); // set of functions that bind data to the datagrids

                  dgMoneyContributions.RenderControl(htmltextwriter);
                  dgProductContributions.RenderControl(htmltextwriter);
                  dgStockContributions.RenderControl(htmltextwriter);
                  dgThriftContributions.RenderControl(htmltextwriter);
                  ggInKindContributions.RenderControl(htmltextwriter);

                  Response.Write (stringwriter.ToString ());
                  Response.End() ;

when i test i get the exception

System.Web.HttpException: Control 'dgMoneyContributions__ctl1__ctl0' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server.

My Page contains stylesheet references and the datagrids themselves contain a hyperlink column. Is this causing the error, if so how to proceed with the export function?

Thanks in advance

Avatar of nparthi
nparthi

The excel shoudl import the column even if it is a hyperlink.
Check out this article

http://www.dotnetjohn.com/articles/articleid36.aspx

http://www.dotnetjohn.com/articles/articleid78.aspx
Avatar of Sujinig

ASKER

I figured why there was an error, it was due to Paging and Sorting. Once I removed the Paging and Sorting it works Fine.

Thanks Anyway!
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial