對于GridView導出的內容的代碼大致如下:
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.grid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
//grid1為表格的ID
注:藍色標識代碼為出錯的那行代碼。
OK,好不容易敲完代碼,運行程序測試。蹬,報錯了。
問題一:類型"Grid1"的控件"gvCompareDetail"必須放在具有 runat=server 的窗體標記內。
注:Grid1為表格的ID。
查找網上的解決大致為:
1)把Grid放到
間。2)給Grid加標記runat="server"。
查明前臺Grid確實有加標記runat="server"的,而且表格是放在form中的。
解決方法:在后臺代碼加上以下重寫方法
public override void VerifyRenderingInServerForm(Control control)
{ }
查找MSDN說明,該函數的作用在于:確認在運行時為指定的 ASP.NET 移動控件呈現 Form 控件。
語法:
public override void VerifyRenderingInServerForm(
Control control
)
參數
control
類型:System.Web.UI..::.Control
必須位于 Form 控件中的 ASP.NET 移動控件。
#p#副標題#e#
備注
如果在運行時控件未包含在 Form 中,則此方法將重寫 Page..::.VerifyRenderingInServerForm 方法以引發(fā)異常。
如果回發(fā)或使用客戶端腳本的服務器控件沒有包含在 HtmlForm 服務器控件 (
相關閱讀
熱門文章
誅仙3飛升任務怎么做-誅仙3飛升任務流程最新2022
鐘離圣遺物推薦-原神鐘離圣遺物詞條
解決方法:應用程序“DEFAULT WEB SITE”中的服務器錯誤
使用aspnet_regiis.exe 重新注冊.NET Framework
人氣排行 誅仙3飛升任務怎么做-誅仙3飛升任務流程最新2022 asp.net表單提交方法GET\POST 在ASP.NET中如何判斷用戶IE瀏覽器的版本 Asp.net中messagebox的實現方法 Asp.net中的web.config配置 在ASP.NET MVC中實現大文件異步上傳 asp.net獲取URL和IP地址 FileUpload上傳多文件出現錯誤的解決方法