欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Asp.net中的GridView導(dǎo)出遇到的兩個(gè)問(wèn)題和解決方法

 更新時(shí)間:2009年12月28日 00:28:28   作者:  
Asp.net下GridView導(dǎo)出遇到的兩個(gè)問(wèn)題與解決方法,需要的朋友可以參考一下。
對(duì)于GridView導(dǎo)出的內(nèi)容的代碼大致如下:
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

注:藍(lán)色標(biāo)識(shí)代碼為出錯(cuò)的那行代碼。
OK,好不容易敲完代碼,運(yùn)行程序測(cè)試。蹬,報(bào)錯(cuò)了。
問(wèn)題一:類型“Grid1”的控件“gvCompareDetail”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)。
注:Grid1為表格的ID。

查找網(wǎng)上的解決大致為:
1)把Grid放到<form runat="server"></form>間。
2)給Grid加標(biāo)記runat="server"。
查明前臺(tái)Grid確實(shí)有加標(biāo)記runat="server"的,而且表格是放在form中的。
解決方法:在后臺(tái)代碼加上以下重寫(xiě)方法
public override void VerifyRenderingInServerForm(Control control)
{ }
查找MSDN說(shuō)明,該函數(shù)的作用在于:確認(rèn)在運(yùn)行時(shí)為指定的 ASP.NET 移動(dòng)控件呈現(xiàn) Form 控件。
語(yǔ)法:
C#
復(fù)制代碼 代碼如下:

public override void VerifyRenderingInServerForm(
    Control control
)

參數(shù)
control
類型:System.Web.UI..::.Control
必須位于 Form 控件中的 ASP.NET 移動(dòng)控件。
備注
如果在運(yùn)行時(shí)控件未包含在 Form 中,則此方法將重寫(xiě) Page..::.VerifyRenderingInServerForm 方法以引發(fā)異常。
如果回發(fā)或使用客戶端腳本的服務(wù)器控件沒(méi)有包含在 HtmlForm 服務(wù)器控件 (<form runat="server">) 標(biāo)記中,它們將無(wú)法正常工作。這些控件可以在呈現(xiàn)時(shí)調(diào)用該方法,以在它們沒(méi)有包含在 HtmlForm 控件中時(shí)提供明確的錯(cuò)誤信息。
開(kāi)發(fā)自定義服務(wù)器控件時(shí),通常在為任何類型的輸入標(biāo)記重寫(xiě) Render 方法時(shí)調(diào)用該方法。這在輸入控件調(diào)用 GetPostBackEventReference 或發(fā)出客戶端腳本時(shí)尤其重要。復(fù)合服務(wù)器控件不需要作出此調(diào)用。
OK,加上以上的函數(shù),編譯運(yùn)行調(diào)試。暈,又出現(xiàn)了別外一個(gè)錯(cuò)誤。

問(wèn)題二:只能在執(zhí)行 Render() 的過(guò)程中調(diào)用 RegisterForEventValidation。

看樣子,以上加的那個(gè)函數(shù)并沒(méi)有徹底解決問(wèn)題了。
經(jīng)過(guò)一番搜索和嘗試,終于把問(wèn)題解決了。

解決方法1:把上面的函數(shù)VerifyRenderingInServerForm去掉,在導(dǎo)出代碼中,動(dòng)態(tài)添加一個(gè)Form對(duì)象,一個(gè)Page對(duì)象,把表格加入它,并把Form添加給Page。
導(dǎo)出的代碼如下:
復(fù)制代碼 代碼如下:

Page p=new Page();
HtmlForm form=new HtmlForm();
Grid1.EnableViewState = false;
p.EnableEventValidation = false;
p.DesignerInitialize();
form.Controls.Add(Grid1);
p.Controls.Add(form);
StringBuilder sb=new StringBuilder();
StringWriter sw=new StringWriter(sb);
p.RenderControl(sw);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
Response.Charset = "UTF-8";
Response.ContentEncoding = Encoding.Default;
Response.Write(sb.ToString());
Response.End();

解決方法2:修改web.config(不推薦)<pages enableEventValidation ="false" ></pages>

相關(guān)文章

最新評(píng)論