将GridView数据导出到Excel工作表出现错误

Shreyas achar

我有一个asp.net应用程序,在其中显示网格视图,其代码如下所示

 <center><asp:GridView ID="GridView1" runat="server" 
        RowStyle-HorizontalAlign="Center" CellPadding="4" ForeColor="#333333" 
         GridLines="None">
 <AlternatingRowStyle BackColor="White" />
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" BackColor="#FFFBD6" ForeColor="#333333"></RowStyle>
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>
 </center>
 <center> <table>
 <tr>
 <td>
    <asp:Button ID="btnWordConvert" runat="server" Text="Button" 
        onclick="btnWordConvert_Click" />
</td>
</tr>
</table>
</center>

在cs中

 protected void btnWordConvert_Click(object sender, EventArgs e)
{
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    Response.ClearContent();
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.doc"));
    Response.Charset = "";
    Response.ContentType = "application/ms-word";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();

}

我在GridView1.RenderControl(htw);网上遇到错误,错误是Control 'ctl00_ContentPlaceHolder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.任何帮助。

塞米·雅格乔格鲁(Seemi Yagcioglu)

得到这个的原因是,通过调用GridView.RenderControl(HtmlTextWriter)表单标记之外呈现了一个服务器控件

您可以通过重写VerifyRenderingInServerForm来避免这种情况

看看这个

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章