简体   繁体   English

类型为“ GridView”的控件“ MainContent_GridView1”必须置于带有runat = server的表单标记中

[英]Control 'MainContent_GridView1' of type 'GridView' must be placed inside a form tag with runat=server

I have added iTextSharp to "Project>...Add References" and added them to using: 我已经将iTextSharp添加到“项目> ...添加引用”,并将它们添加到了使用:

using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

I get the error code at line "GridView1.RenderControl(hw);". 我在“ GridView1.RenderControl(hw);”行得到错误代码。

How do you add a runat="server" thru Gridview and the HtmlTextWriter: 如何通过Gridview和HtmlTextWriter添加runat =“ server”:

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

Button1_Click Code: Button1_Click代码:

protected void Button1_Click(Object sender,System.EventArgs e)
    {
        string connStr = ConfigurationManager.ConnectionStrings["PDFMDF"].ConnectionString;
        DataSet ds = new DataSet();
        try
        {
            string cmdStr = "SELECT * FROM [GridviewTable];";
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
                {
                    conn.Open();
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.Fill(ds);
                        GridView1.DataSource = ds;
                        GridView1.DataBind();
                    }
                    conn.Close();
                    cmd.Dispose();
                    conn.Dispose();
                }
            }
        }
        catch  (Exception ex)
        {
            TextBox1.Text = ex.ToString();
        }

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=this.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document PDFdoc = new Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F);
        iTextSharp.text.html.simpleparser.HTMLWorker htmlparser =   new iTextSharp.text.html.simpleparser.HTMLWorker(PDFdoc);
        PdfWriter.GetInstance(PDFdoc, Response.OutputStream);
        PDFdoc.Open();
        htmlparser.Parse(sr);
        PDFdoc.Close();
        Response.Write(PDFdoc);
        Response.End();
    }

You can do this in your markup code. 您可以在标记代码中执行此操作。 Ex. 例如

 <asp:GridView ID="GridView1" runat="server">

in my markup allready have code runat="server" add this code to your source 在我的标记中,已经准备好代码runat =“ server”将此代码添加到您的源代码中

    public override void VerifyRenderingInServerForm(Control control)
    {
        return;
    }

it solving the problem 它解决了问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 必须将带有&#39;GridView&#39;类型的控件&#39;GridView1&#39;放在带有runat = server的表单标签内。 - Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.? 类型为&#39;TextBox&#39;的控件&#39;MainContent_forenameTxt&#39;必须放置在带有runat = server的表单标签中 - Control 'MainContent_forenameTxt' of type 'TextBox' must be placed inside a form tag with runat=server GridView 必须放在带有 runat="server" 的表单标签内,即使 GridView 位于表单标签内 - GridView must be placed inside a form tag with runat="server" even after the GridView is within a form tag 类型[…]的控件[…]必须放置在带有runat = server的表单标记中-但已经是 - Control […] of type […] must be placed inside a form tag with runat=server - but already is 类型为&#39;TextBox&#39;的控件&#39;0&#39;必须放置在带有runat = server的表单标记中 - Control '0' of type 'TextBox' must be placed inside a form tag with runat=server 类型为“ HtmlEditorExtender”的控件必须放在带有runat = server的表单标签中 - Control of type 'HtmlEditorExtender' must be placed inside a form tag with runat=server 在服务器上运行的表单内的Gridview引发““ GridView”必须放置在带有runat = server的表单标记内” - Gridview inside of form running at server throws “'GridView' must be placed inside aform tag with runat=server” 类型为&#39;TextBox&#39;的控件&#39;ctl00_txtDebug&#39;必须放置在带有runat = server的表单标记中 - Control 'ctl00_txtDebug' of type 'TextBox' must be placed inside a form tag with runat=server 类型为“ RadioButton”的控件“ ctl00”必须置于带有runat = server的表单标记中 - Control 'ctl00' of type 'RadioButton' must be placed inside a form tag with runat=server “TextBox”类型的控件“windowMasterSearchBox”必须放置在带有 runat=server 的表单标记中 - Control 'windowMasterSearchBox' of type 'TextBox' must be placed inside a form tag with runat=server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM