我正在开发一个项目,我想下载gridview行数据,但它对我不起作用。这是我用于下载的代码:
string fileName = "chhattisgarhishafte" + DateTime.Now.ToString() + ".doc";
GridView1.DataSource = dtD;
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderBeginTag(hw);
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
GridView1.RenderEndTag(hw);
Response.Flush();
Response.End();
stD是datatble,用于存储gridview选定的行。
错误是:
Control 'ctl00_ContentPlaceHolder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
答案 0 :(得分:0)
请在母版页的表格标签中使用runat =“server”
答案 1 :(得分:0)
使用此代码,它在我的应用程序中工作
if (gv.Rows.Count > 0)
{
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
//Get the HTML for the control.
gv.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + strFileName);
EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}