简体   繁体   中英

Control 'testGridview' of type 'SPGridView' must be placed inside a form tag with runat=server

I have a spgridview and want to export its content to pdf. I am using itextsharp for the same. But error is encountered at rendercontrol call. I have already tried the solutions mentioned in the below thread. Question Also I have tried using

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

but it doesnot work beacuse I am writing the code in usercontrol. Here is my ascx code.

<SharePoint:SPGridView ID="domainGridview" AllowPaging="false" runat="server" AutoGenerateColumns="False"
ShowHeader="true" ShowHeaderWhenEmpty="true" AllowSorting="True" GridLines="None" EnabledEventValidation="false">
<AlternatingRowStyle CssClass="ms-alternating" />
<Columns>
<SharePoint:SPBoundField DataField = "SiteName" HeaderText = "Team Site Name" SortExpression = "SiteName"></SharePoint:SPBoundField>
<asp:BoundField DataField = "SiteUrl" HtmlEncode="false" HeaderText = "Team Site URL" SortExpression = "SiteUrl" ></asp:BoundField>
</Columns>
</SharePoint:SPGridView>

<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />

This is my usercontrol.ascx.cs code

 protected void btnExport_Click(object sender, EventArgs e)
    {

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        UserControl myControl = (UserControl)LoadControl("DomainUserWebPartUserControl.ascx");
        domainGridview.RenderControl(hw);

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition",
            "attachment;filename=DataTable.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);



        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }

This is the exception i am getting

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

Finally I got my solution. I am using a different approach alltogether.

 protected void btnExport_Click(object sender, EventArgs e)
    {
        dtUIExport = GetData();
        //Creating iTextSharp Table from the DataTable data
        PdfPTable pdfTable = new PdfPTable(dtUIExport.Columns.Count - 1);

        pdfTable.DefaultCell.BorderWidth = 2;
        int[] widths = new int[dtUIExport.Columns.Count - 1];
       //setting font
        BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\ARIALUNI.TTF", BaseFont.IDENTITY_H, true);

        //setting width
        for (int x = 0; x < domainGridview.Columns.Count -1; x++)
        {
          widths[x] = (int)domainGridview.Columns[x].HeaderText.Length + 100;              
        }
        pdfTable.SetWidths(widths);

        //setting headers
        for (int i = 0; i < dtUIExport.Columns.Count; i++)
        {
            if (i == 1)
            { continue; }
            iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
            font.Color = new BaseColor(domainGridview.HeaderStyle.ForeColor);
            string cellText = Server.HtmlDecode(dtUIExport.Columns[i].ToString());               
             iTextSharp.text.pdf.PdfPCell cell = new PdfPCell(new Phrase(12, cellText, font));
             pdfTable.AddCell(cell);     
        }

        //copying the data to pdftable
        for (int i = 0; i < dtUIExport.Rows.Count; i++)
        {
            for (int j = 0; j < dtUIExport.Columns.Count; j++)
            {
                if (j == 1)
                { continue; }

                    string cellText = Server.HtmlDecode(dtUIExport.Rows[i][j].ToString());
                    iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
                    font.Color = new BaseColor(domainGridview.RowStyle.ForeColor);
                    iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));
                    pdfTable.AddCell(cell);
            }
        }
       //creating the PDF DOC
        Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(pdfTable);
        pdfDoc.Close();

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfTable);
        Response.End();
    }        
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM