You must Sign In to post a response.
  • Category: ASP.NET

    A page can have only one server-side Form tag.

    I am generatiing bill in pdf. all is working fine but if i click the export to pdf button it throws the following error.

    A page can have only one server-side Form tag.

    my aspx code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="QuotationBill.aspx.cs" Inherits="QuotationBill" %>

    <!DOCTYPE html>

    <html xmlns="">
    <head runat="server">
    <form id="form1" >

    <legend>Bind and Export GridView data to PDF in</legend>
    <asp:GridView ID="cgv" runat="server" AllowPaging="True" AutoGenerateColumns="False"
    GridLines="None" Width="100%" CellPadding="4" ForeColor="#333333" ShowHeader="false">

    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <ItemTemplate >
    <table style="padding-left:170px;">
    <asp:Label ID="Label1" Text="DATE" runat="server" Font-Bold="true"></asp:Label>
    <span style="float:right">: </span>
    <asp:Label ID="Label2" Text='<%#Eval("QuotationDate") %>' runat="server" Font-Bold="true" ForeColor="#000000"></asp:Label>

    <td style="padding-left:70px;">
    <asp:Label ID="Label66" Text="Station" runat="server" Font-Bold="true"></asp:Label>
    <span style="float:none">: </span>
    <asp:Label ID="Label68" Text="Banglore" runat="server" Font-Bold="true" ForeColor="#000000"></asp:Label>


    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <asp:Button ID="btnExportToPdf" runat="server" Text="Export To PDF File" OnClick="btnExportToPdf_Click" />


    M C# code :
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.Sql;
    using System.Data;
    using System.Configuration;
    using System.IO;
    using System.Text;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.html.simpleparser;
    using System.Net;
    using System.Net.Mail;
    using System.Web.UI.HtmlControls;

    public override void VerifyRenderingInServerForm(Control control)
    //It solves the error "Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."

    protected void btnExportToPdf_Click(object sender, EventArgs e)
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=MyPdfFile.pdf");
    StringWriter strWrite = new StringWriter();
    HtmlTextWriter htmWrite = new HtmlTextWriter(strWrite);
    HtmlForm frm = new HtmlForm();
    frm.Attributes["runat"] = "server";
    StringReader sr = new StringReader(strWrite.ToString());
    Document pdfDoc = new Document(PageSize.A4, 8f, 8f, 8f, 2f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    catch (Exception ex) { }

    in this line


    also throw this error

    Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.html.simpleparser.TableWrapper'.

    i cant able to resolve this error . i get confused.
    So plaese help mr to i am resolve the error

    Thanking you.
  • #765759
    Thanks for all of you. i got output for my posted question.


  • #765763
    Hi Paul,

    Coming to your first problem

    1) A page can have only one server-side Form tag.

    A) If you are using form tag in master page, no need to use the form tag again in your child pages. If you are using then you may face this type of problems, remove the form tag, if you are inherit master page use content template and design your page inside that template only.

    2) Unable to cast object of type

    A) "htmlparser" class is available in 2 namespaces, one is "'iTextSharp.text.html.simpleparser.CellWrapper' and another one is "'iTextSharp.text.html.simpleparser.TableWrapper'", while calling the class CLR gets confused which one needs to refer, if you want to overcome this before calling the class you should provide the namespace too.

    if "htmlparser" class you are looking for TableWrapper namespace then use below sample.


    same way htmlparser pointing CellWrapper namespace then use that, in that case you won't get this error.

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

Sign In to post your comments