我正在尝试将网格导出到 SharePoint Web 部件解决方案中 ascx 页面上的 excel。它引发以下错误
Control 'ctl00_ctl24_g_20574c20_e209_4887_ab02_83ee55a79fc5_ctl00_gdReport' of type 'GridView' must be placed inside a form tag with runat=server.
为了避免这个错误,我必须使用以下方法,但是编译器会抛出一个错误,说“没有找到合适的方法来覆盖”
public override void VerifyRenderingInServerForm(Control control) { }
完整代码:
protected void btnExcelExport_Click(object sender, EventArgs e)
{
PrepareForExport(gdSharedReport);
ExportToExcel();
}
private void ExportToExcel()
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Report.xls");
Response.Charset = String.Empty;
Response.ContentType = "application/ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(stringWriter);
gdSharedReport.RenderControl(HtmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
private void PrepareForExport(Control ctrl)
{
//iterate through all the grid controls
foreach (Control childControl in ctrl.Controls)
{
//if the control type is link button, remove it
//from the collection
if (childControl.GetType() == typeof(LinkButton))
{
ctrl.Controls.Remove(childControl);
}
//if the child control is not empty, repeat the process
// for all its controls
else if (childControl.HasControls())
{
PrepareForExport(childControl);
}
}
}