I have a DataGrid that I am trying to export when an ASP.NET Button is clicked.
The exact error is:
Control 'ctl00_body_RollupDG_ctl02_btnShowGLdetail' of type 'LinkButton' must be placed inside a form tag with runat=server.
I found similar questions on here, but all seem to indicate that this comes from the ASP.NET control NOT being placed within a ContentPlaceHolder or a ContentPlaceHolder NOT being placed within a RunAt Server form.
I have both of these, so that is not the case here.
My ExportExcelFile method catches the HttpException at RenderControl() (as shown in the screenshot above). That code is as follows:
protected void ExportExcelFile(object Sender, EventArgs e) { //export to excel
var grdResults = (periodCriteria.SelectedValue == "year") ? RollupDG : QuarterDG;
var response = HttpContext.Current.Response;
response.Clear();
response.Charset = String.Empty;
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment; filename=GlBudgetReport.xls");
using (var sw = new StringWriter()) {
using (var htw = new HtmlTextWriter(sw)) {
grdResults.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
As shown in my Find Results box below, there are several ASP.NET LinkButton controls, but each of them does contain the runat="server"
clause.
It appears the LinkButton data in the DataGrid has difficulty rendering as Plain Text.
If not, is there something in my export method that can be configured so that the DataGrid data is all interpreted as text?
I could create a blank DataGrid, then walk through the filled DataGrid, writing in only the text of each field - but I only want to do this if it is absolutely necessary.
I believe you just need to override the method that checks for a form. You don't need to add any logic to the method. Just add the following to your code behind:
public override void VerifyRenderingInServerForm(Control control) { }
ETA: You may also need to set EnableEventValidation="false"
in the <%@Page />
element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With