Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Iam getting this error while transferring data from gridview to excel, can any one suggest me the solution"RegisterForEventValidation can only be called during Render();"

I got a solution that i should do EnableEventValidation=false and generate the event VerifyRenderingInServerForm(Control control) in my code...i did this too..
now i am not getting any error...but excel file is not getting generated..help...
Posted
Comments
bbirajdar 31-Dec-12 3:43am    
And the question is ?
nileshpujari 31-Dec-12 4:01am    
aspnet_regiis..
my question is that why my excel file not getting generated...
WajihaAhmed 31-Dec-12 3:55am    
It would be helpful if you provide your code.
nileshpujari 31-Dec-12 4:00am    
The following is my code....

protected void btngenexcel_Click(object sender, EventArgs e)
{
ExportXls(grdMails,"MailsData");
grdMails.DataSource = null;
grdMails.DataBind();
modalShowMails.Hide();
}


public static void ExportXls(GridView gridName, string filename)
{
string attachment = "attachment; filename=" + filename + ".xls";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/ms-excel";
StringWriter sWriter = new StringWriter();

HtmlTextWriter htwWriter = new HtmlTextWriter(sWriter);

gridName.RenderControl(htwWriter);
HttpContext.Current.Response.Write(sWriter.ToString());
HttpContext.Current.Response.End();

}

}
nileshpujari 31-Dec-12 4:09am    
WajihAhmed... r u der...??

I had once done this with some help from here:

http://stackoverflow.com/questions/8908363/exporting-gridview-to-xls-paging-issue[^]

See if that helps you too :) Looking forward for your comments.
 
Share this answer
 
v2
The button that is supposd to generate the excel file..;place that button in the <asp:postbacktrigger xmlns:asp="#unknown"> tab of the <triggers> tab...
and if ur grid is doing paging then while exporting the data into excel use the Datatable instead of using the gridview coz if u use gridview then it will not generate proper excel file...
or u can do this..while generatin excel again get the data from the DB...
 
Share this answer
 
Comments
WajihaAhmed 1-Jan-13 8:32am    
Kindlu accept this Solution 3 as the answer, so that it can be concluded that your problem is solved.

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