問題
GridView 匯出Excel 發生Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
解決方法
語法如下:
protected void btnExportExcel_Click(object sender, EventArgs e) { if (GridView1.Rows.Count > 0) { string attachment = string.Format("attachment; filename=allocationInvoice{0}.xls" , DateTime.Now.ToString(("yyyyMMddHHmmss"))); Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); GridView1.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); } }但是會發生錯誤下:
Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
解決方式override 原有的function:
public override void VerifyRenderingInServerForm(Control control)
{
}
沒有留言:
張貼留言