我想導出網格以在SharePoint Web部件解決方案中的ascx頁上擅長。它拋出以下錯誤導出網格在SharePoint 2010中的ascx頁上擅長
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);
}
}
}
對於第一個錯誤,您在'.aspx'頁面中缺少'runat =「Server」'屬性。第二,你需要發佈更多的相關代碼。你確定該方法可以/需要被覆蓋嗎? – Brian 2013-03-07 16:44:29
@Brian,增加了完整的代碼。請注意我沒有在窗體上使用VerifyRenderingInServerForm()。另外,我的網格放置在ascx頁面上。 – Rishi 2013-03-07 17:08:31
也許你需要。 – jp2code 2013-03-07 17:14:50