Click here to Skip to main content
15,906,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using following code for export to excel from a data from a Gridview control.
VB
lblError.Visible = false;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=BorrowerReport.xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving than
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
StringWriter stringWrite = new System.IO.StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
datagrid.RenderControl(htmlWrite);
            
Response.Write(stringWrite.ToString());
Response.End();

But now add a linkbutton as item template of this control .Then show a error as

Control 'ctl00_Contentplaceholder2_grdBorrowerMaster_ctl02_LinkButton1' of type 'LinkButton' must be placed inside a form tag with runat=server.

So How can I export?
Posted
Updated 26-Jan-11 22:37pm
v3
Comments
JF2015 27-Jan-11 1:34am    
Added code formatting.
Sandeep Mewara 27-Jan-11 1:56am    
Looks like you have shared a server side code and the error lies on client side.
can you clarify where exactly is your link button and the HTML tag you used?

Try adding below code in your code behind :

C#
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}
 
Share this answer
 
Comments
vipinsethumadhavan 27-Jan-11 1:56am    
I am also used this.
Looks like the issue is with rendering.This below article discuss on similar issue.Have a look at this on rendering the html part,you may get some idea.

http://dotnetforyou.wordpress.com/2011/01/23/dynamically-loading-usercontrol-in-asp-net-page-resulting-exception-control-button1-of-type-button-must-be-placed-inside-a-form-tag-with-runatserver/[^]

This discuss on loading UserControl,replace that with DataGrid & try.
 
Share this answer
 
v2
 
Share this answer
 
Comments
vipinsethumadhavan 27-Jan-11 3:05am    
Then now shown error as
RegisterForEventValidation can only be called during Render();
I am not sure about problem which you getting so you may navigate the given links to get simple way to export data from datagridview to excel
Link1[^]
Link2[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900