I tried to follow up Ram's suggestion and exported data in a GridView to a word documents by following code snippet:

 

GridView control "must be placed inside a form tag with runat=server"GridView control "must be placed inside a form tag with runat=server"代码
 1 protected void Button1_Click(object sender, EventArgs e)
 2 {
 3     Response.Clear();
 4     Response.Buffer = true;
 5     Response.AddHeader("content-disposition""attachment;filename=export.docx");
 6     Response.ContentEncoding = System.Text.Encoding.UTF8;
 7     Response.ContentType = "application/vnd.word";
 8     System.IO.StringWriter strWriter = new System.IO.StringWriter();
 9     System.Web.UI.HtmlTextWriter htmlTxtWriter = new HtmlTextWriter(strWriter);
10     this.gvCategory.RenderControl(htmlTxtWriter);
11     Response.Output.Write(strWriter.ToString());
12     Response.Flush();
13     Response.End();
14 

 

After building the project and try to view it in browser, however, the browser just told me the following message when clicking the Export data button:
 GridView control "must be placed inside a form tag with runat=server"

 

 

 

 

 

 

 

 

 

 

 

 

After a quick search, I found: http://rstew.blogspot.com/2007/10/gridview-must-be-placed-inside-form-tag.html, great post, override the VerifyRenderingInServerForm method, the process works happily!

public override void VerifyRenderingInServerForm(Control control)

{

      return;

}

 

转载于:https://www.cnblogs.com/RoahnLuo/archive/2009/11/27/1612262.html

相关文章:

  • 2021-07-05
  • 2021-04-04
  • 2021-05-18
  • 2021-05-01
  • 2021-05-18
  • 2021-11-30
  • 2021-07-15
猜你喜欢
  • 2021-09-10
  • 2021-12-08
  • 2021-05-20
  • 2021-05-14
  • 2021-11-09
  • 2021-06-24
相关资源
相似解决方案