简体   繁体   English

将gridview数据导出到Excel工作表会出错

[英]Export gridview data to a excel sheet is giving error

I have a asp.net application where in i am displaying a grid view whose code is shown below 我有一个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>

in cs 在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();

}

i am getting error on GridView1.RenderControl(htw); 我在GridView1.RenderControl(htw);上遇到错误GridView1.RenderControl(htw); line and the error is Control 'ctl00_ContentPlaceHolder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server. 行和错误是Control 'ctl00_ContentPlaceHolder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server. Any help are appreciated. 任何帮助表示赞赏。

The reason you are getting this is, a server control is rendered outside of a form tag by calling GridView.RenderControl(HtmlTextWriter) . 您得到这个的原因是,通过调用GridView.RenderControl(HtmlTextWriter)窗体标记之外呈现服务器控件。

You could avoid this by overriding VerifyRenderingInServerForm . 您可以通过覆盖VerifyRenderingInServerForm来避免这种情况

Have a look at this . 看看这个

Put your code inside form tag. 将代码放在表单标记中。

<form id="form1" runat="server">

   //put your code here
</form>

Please make sure that your gridview is inside form tag. 请确保您的gridview位于表单标记内。

 <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" 
        RowStyle-HorizontalAlign="Center" CellPadding="4" ForeColor="#333333" 
         GridLines="None">
                        ... ... ... 
    </asp:GridView>
 </form>

put following method 按照以下方法

public override void VerifyRenderingInServerForm(Control control)
{
      /* Verifies that the control is rendered */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM