Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
error = Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
C#
using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                {
                    GridView1.AllowPaging = false;
                   
                    this.DataBind();
                    GridView1.RenderControl(hw);
                    StringReader sr = new StringReader(sw.ToString());
                    Document pdfdoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
                    HTMLWorker htmlparser = new HTMLWorker(pdfdoc);
                    PdfWriter.GetInstance(pdfdoc, Response.OutputStream);
                    pdfdoc.Open();
                    htmlparser.Parse(sr);
                    pdfdoc.Close();

                    Response.ContentType = "Application/pdf";
                    Response.AddHeader("content-disposition"attachemnt;FileName=Spj");
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.Write(pdfdoc);
                    Response.End();



                }
            }
Posted
Updated 15-Feb-15 19:35pm
v2

1 solution

Refer thiz link..

1) How to convert GridView data to Excel, PDF, Word file using C#
2)how to export gridview data to PDF using asp.net or Export gridview data to PDF using asp.net
3)How to export GridView data to PDF file in ASP.Net

You are using ur gridview outside the form tag so put ur gridview inside the form tag.
Eg:
ASP.NET
<form  runat="server">//placed ur gridview here</form>
 
Share this answer
 
v2
Comments
Deepu S Nair 16-Feb-15 1:25am    
Good links.+5
[no name] 16-Feb-15 1:27am    
Thank u Deepu :)
Shrikesh_kale 16-Feb-15 3:24am    
another error ocuered=====" The document has no pages."
[no name] 16-Feb-15 7:18am    
Refer..
1)http://stackoverflow.com/questions/8355308/error-converting-to-pdf-the-document-has-no-pages-at-document-close
2)http://stackoverflow.com/questions/24075844/itextsharp-error-the-document-has-no-pages
and change the line of ur code as
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900