Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am getting this error on excel sheet..
System.InvalidOperationException: RegisterForEventValidation can only be called during Render();


here my code

protected void btnexport_Click(object sender, ImageClickEventArgs e)
{
try
{

Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Planwise Report.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

btnview_Click(this, null);
GridView1.AllowPaging = false;
//Change the Header Row back to white color
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
}
int j = 1;
//This loop is used to apply stlye to cells based on particular row
foreach (GridViewRow gvrow in GridView1.Rows)
{
gvrow.BackColor = Color.White;
if (j <= GridView1.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());

Response.Flush();

}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
Response.End();

}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}

whats my mistake..?
Posted

1 solution

Similar question with answer one day ago
Export excel problem in asp.net[^]

EDIT
-------------------------
Jhonnie Walker wrote:
but its exporting only whats on the page..its not exporting all over Grid eg. i have 500 records and im using paging like 50 per page .its exporting only 50 records .any solutions ?sir..
Check this
Export GridView ( all pages ) with Paging enabled ( AllowPaging ) to Excel in ASP.Net[^]
 
Share this answer
 
v2
Comments
King Fisher 7-Nov-13 7:11am    
http://www.codeproject.com/Questions/45450/RegisterForEventValidation-can-only-be-called-duri


i checked this ..this is okay..but is this a good way to solve this problem.and can you explain why for this error ?..
thatraja 7-Nov-13 7:20am    
Probably you should read about RegisterForEventValidation & EnableEventValidation.

Good way? why do you think it's a bad way? please give us details.
Thanks7872 7-Nov-13 7:20am    
Yes,this is very good way. Follow it.
King Fisher 7-Nov-13 7:42am    
but its exporting only whats on the page..its not exporting all over Grid

eg.
i have 500 records and im using paging like 50 per page .its exporting only 50 records .any solutions ?sir..
thatraja 7-Nov-13 7:53am    
Check updated 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