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="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" >
    <div>

    <legend>Bind and Export GridView data to PDF in asp.net</legend>
    <table>
    <tr>
    <td>
    <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" />
    <Columns>
    <asp:TemplateField>
    <ItemTemplate >
    <table style="padding-left:170px;">
    <tr>
    <td>
    <asp:Label ID="Label1" Text="DATE" runat="server" Font-Bold="true"></asp:Label>
    <span style="float:right">: </span>
    </td>
    <td>
    <asp:Label ID="Label2" Text='<%#Eval("QuotationDate") %>' runat="server" Font-Bold="true" ForeColor="#000000"></asp:Label>

    </td>
    <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>

    </td>
    </tr>

    </table>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <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:GridView>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Button ID="btnExportToPdf" runat="server" Text="Export To PDF File" OnClick="btnExportToPdf_Click" />
    </td>
    </tr>
    </table>

    </div>
    </form>
    </body>
    </html>


    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)
    {
    try
    {
    Response.ClearContent();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=MyPdfFile.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter strWrite = new StringWriter();
    HtmlTextWriter htmWrite = new HtmlTextWriter(strWrite);
    HtmlForm frm = new HtmlForm();
    grEmp.Parent.Controls.Add(frm);
    frm.Attributes["runat"] = "server";
    frm.Controls.Add(grEmp);
    frm.RenderControl(htmWrite);
    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);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.Flush();
    Response.End();
    }
    catch (Exception ex) { }
    }

    in this line

    htmlparser.Parse(sr);

    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.
    Paul.S
  • #765759
    Thanks for all of you. i got output for my posted question.

    Paul.S

  • #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.

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

    iTextSharp.text.html.simpleparser.TableWrapper.htmlparser

    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.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments