0

我正在做一个项目,我想下载 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 是存储 gridview 选定行的数据表。

错误是:

Control 'ctl00_ContentPlaceHolder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
4

2 回答 2

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();
            }
于 2013-09-18T12:21:09.423 回答
0

请在母版页的表单标签中使用 runat="server"

于 2013-09-18T12:07:24.767 回答