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 は、グリッドビューで選択された行を格納する datatble です。

エラーは次のとおりです。

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 に答える