Click here to Skip to main content
12,358,497 members (60,471 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Gridview
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 30-Dec-12 21:41pm
Comments
aspnet_regiis -i 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...
WajihAhmed 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...??
WajihaAhmed 31-Dec-12 4:14am
   
Yes :)
nileshpujari 31-Dec-12 4:25am
   
hey dude...that problem is solved..but another one has arisen...excel file is getting generated...but i have used paging while showing the data that i wanted to export in excel...so the problem is the entire data is not comin in excel..instead only the first data is cumin...wat mite b the prob..
nileshpujari 31-Dec-12 3:59am
   
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:00am
   
aspnet_regiis..
my question is that why my excel file not getting generated...
aspnet_regiis -i 31-Dec-12 4:12am
   
remove "HttpContext.Current.Response.End();" from your code
nileshpujari 31-Dec-12 4:26am
   
hey my that problem is solved..but another one has arisen...excel file is getting generated...but i have used paging while showing the data that i wanted to export in excel...so the problem is the entire data is not comin in excel..instead only the first data is cumin...wat mite b the prob..
aspnet_regiis -i 31-Dec-12 4:44am
   
Tell us how the problem was solved and which solution worked for you.....You need to export the datatable instead of the gridview....
nileshpujari 31-Dec-12 5:13am
   
yup u r rite...dat was also solved using datatable.... and the previos prob was solved in foll way...
the button that was supposd ti generate excel file that button should be taken in <Triggers> tab and in that <asp:Postbacktrigger>...
thanx...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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 Smile | :) Looking forward for your comments.
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

The button that is supposd to generate the excel file..;place that button in the <asp:postbacktrigger xmlns:asp="#unknown"> tab of the 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...
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100