Control 'GridView1' of type 'GridView' must be placed inside a form tag with
怎么解决?

解决方案 »

  1.   

    上边写的简单了,补充一下:
    从网上找到一个将GridView中的数据输出到Excel文件的方法:
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", "attachment;filename=test.xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/ms-excel
            this.GridView1.EnableViewState = false;
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            this.GridView1.RenderControl(oHtmlTextWriter);   
            Response.Write(oStringWriter.ToString());
            Response.End();在页面运行时出现如下错误:
    Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
    我的GridView1明明就放在一个form里边,而且该form和GridView1都有“runat=server”属下,
      

  2.   

    把前台的代码贴上来看看。
    提示的错误是:要把GridView放在runat=server属性的form里面
      

  3.   

    前台代码如下:
    <div>
        <form id="form1" runat="server">
            <asp:Button ID="btnExcel" runat="server" Text="输出到Excel" CssClass="greenButtonCss" OnClick="btnExcel_Click" />&nbsp;
            查询条件:<asp:DropDownList ID="ProjName" runat="server" Width="234px">
            </asp:DropDownList>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            </asp:DropDownList>
            <asp:TextBox ID="TextBox1" runat="server" Width="217px">0</asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server" Visible="False"></asp:TextBox>
            <asp:Button ID="btnOK" runat="server" Text=" 查询 " CssClass="greenButtonCss" OnClick="btnOK_Click" />&nbsp;
            <asp:GridView ID="GridView1" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="taskno" Width="100%" CellPadding="4" ForeColor="#333333" BorderStyle="Solid" BorderWidth="3px" PageSize="20" runat="server">
                <Columns>
                    <asp:BoundField DataField="taskno" HeaderText="任务编号" />
                    <asp:BoundField DataField="taskname" HeaderText="任务名称" >
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                    </asp:BoundField>
                    <asp:BoundField DataField="ptstate" HeaderText="进度状态" HtmlEncode="False" >
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                    </asp:BoundField>
                    <asp:BoundField DataField="work" HeaderText="工作量" >
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                    </asp:BoundField>
                    <asp:BoundField DataField="complete" HeaderText="完成率" />
                    <asp:BoundField DataField="username" HeaderText="负责人" />
                </Columns>
                <SelectedRowStyle CssClass="SelectedRow" />            
               <FooterStyle CssClass="Footer" />
               <RowStyle CssClass="Row" />
               <PagerStyle CssClass="Pager" />
               <HeaderStyle  CssClass="Header" />
               <AlternatingRowStyle CssClass="AlternatingRow" />
            </asp:GridView>
        </form>
        
            </div>
      

  4.   

    把下面的代码粘贴到你的程序里面就可以了(vb的,你自己改成c#的吧)。
    Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
    End Sub
    还有如果用到AJAX的话,导出按钮一定要在UpdatePanel之外才可以。
      

  5.   

    类型“GridView”的控件“gvUser”必须放在具有 runat=server 的窗体标记内。 
    <body bgcolor="lightblue"> 
    <form id="form1" runat="server"> <asp:GridView ID="gvUser" runat="server" 
      

  6.   

    在你代码加上这个函数。。
    public override void VerifyRenderingInServerForm(Control control)
    {
    }