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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…