遭遇['GridView' must be placed inside a form tag with runat=server.]

  悲剧的一个下午,在帮同事完善离职前留下的项目的时候,碰到这么一个稀奇古怪又不知道怎么解决的问题.

  在使用GridView自带的直接导出Excel的功能的时候,发生了错误:  

  网上查阅了一些资料,但是都不是我的情况,代码很简单,也很明了...

  前台:

View Code
 <form id="form1" runat="server">
<div>
<asp:GridView ID="gdvCustomer" runat="server" AllowPaging="True"
AutoGenerateColumns
="False" DataSourceID="SqlDataSource1" >
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="NAME" HeaderText="NAME" SortExpression="NAME" />
<asp:BoundField DataField="AGE" HeaderText="AGE" SortExpression="AGE" />
<asp:BoundField DataField="CLASS" HeaderText="CLASS" SortExpression="CLASS" />
<asp:BoundField DataField="GRADE" HeaderText="GRADE" SortExpression="GRADE" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString
="<%$ ConnectionStrings:TestAccessDBConnectionString %>"
ProviderName
="<%$ ConnectionStrings:TestAccessDBConnectionString.ProviderName %>"
SelectCommand
="SELECT [ID], [NAME], [AGE], [CLASS], [GRADE] FROM [Customer]">
</asp:SqlDataSource>

<asp:Button ID="btnExport" runat="server" onclick="btnExport_Click"
Text
="Export" />
<br />
</div>
</form>

  后台:  

View Code
    /// <summary>
/// 將Gridivew中的數據導出為Excel
/// </summary>
/// <param name="ctrl">Gridview名稱</param>
/// <param name="FileName">要保存的文件名</param>
public void ExportToExcel(Control ctrl, String FileName)
{
GridView gv = (GridView)ctrl;
gv.AllowPaging = false;//禁用Gridview的分页
gv.AllowSorting = false;//禁用Gridview排序
gv.DataBind();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
gv.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);

gv.RenderControl(hw);
//gv.RenderBeginTag(hw);
//gv.HeaderRow.RenderControl(hw);
//foreach (GridViewRow row in gv.Rows)
//{
// row.RenderControl(hw);
//}
//gv.FooterRow.RenderControl(hw);
//gv.RenderEndTag(hw);

HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();

}

protected void btnExport_Click(object sender, EventArgs e)
{
ExportToExcel(this.gdvCustomer,"Customer.xls");
}

  使用gv.RenderControl(hw)导出Excel以前也都用过的啊,而且同样的方法,在这个页面的上一级跳转页面就有使用,而且导出Excel正常,纠结了我一个小时,实在找不出异常,最后走点弯路,把GridView一行一行的贴到Excel里.

  前台不变,就是后台把GridView写入流的动作改变:

View Code
            gv.RenderBeginTag(hw);
gv.HeaderRow.RenderControl(hw);
foreach (GridViewRow row in gv.Rows)
{
row.RenderControl(hw);
}
gv.FooterRow.RenderControl(hw);
gv.RenderEndTag(hw);

  最后导出的Excel有点丑,表格的框线都不见了。。。只剩下光屁股的数据...唉!

  哪位有解决办法或者曾经类似的解决经历么?求教!  

posted @ 2012-01-19 09:46  liver.wang  阅读(746)  评论(0编辑  收藏  举报