Kavi suja

Kavi suja

  • NA
  • 298
  • 170.9k

call serverside Buttonclick from Javascript

Sep 11 2013 3:32 AM
Hi,
   In my Registration page I want  to generate PDF file after entering the details.  Now I can generate PDF using iTextSharp. But, my problem here is,in Reg.aspx page itself the PDF generated. I want to generate PDF in blank page. How can i do this ? can anyone help me to do this? Here I mention my code.Now i'm trying this using <a /> tag.
In Reg.aspx :
 <asp:Button ID="btnUpload" runat="server" Text="Generate PDF" OnClick="btnUpload_Click" Visible="false"/>
            <a id="pdfconvert" target="_blank" onclick="generatepdf()" runat="server">Convert to PDF</a>
In Javascript :
 function generatepdf() {
            //var btnpdf = document.getElementById("btnUpload")
            var btnpdf = document.getElementById("<%= btnUpload.ClientID %>");
            btnpdf.click();
            // document.getElementById("btnUpload")
        }

In Reg.aspx.cs
protected void btnUpload_Click(object sender, EventArgs e)
        {
            Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);

            try
            {
                PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
                pdfDoc.Open();

                //Set Font Properties for PDF File
                Font fnt = FontFactory.GetFont("Times New Roman", 14);

                PdfPTable PdfTable = new PdfPTable(1);
                //PdfPCell PdfPCell = null;

                //Retrieve content from design side
                string pntype = "Reg.Type  : ";
                string ptype = ddlpatienttype.SelectedItem.Text;
                PdfPCell ptypeval = new PdfPCell(new Phrase(new Chunk(pntype + ptype)));
                PdfTable.AddCell(ptypeval);

                string pnamel = "Name : ";
                string pnamet = "";
                if (rdbtnmr.Checked == true)
                {
                    pnamet = lblsalutmr.Text + " " + txtPFirstName.Text + " " + txtPMiddleName.Text + " " + txtPLastName.Text;
                }
                else if (rdbtnms.Checked == true)
                {
                    pnamet = lblsalutms.Text + " " + txtPFirstName.Text + " " + txtPMiddleName.Text + " " + txtPLastName.Text;
                }
                else
                //if (rdbtnmrs.Checked == true)
                {
                    pnamet = lblsalutmrs.Text + " " + txtPFirstName.Text + " " + txtPMiddleName.Text + " " + txtPLastName.Text;
                }
                //string pnamet = ddlSalutation.SelectedItem.Text + " " + txtPFirstName.Text + " " + txtPMiddleName.Text + " " + txtPLastName.Text;
                PdfPCell pnameval = new PdfPCell(new Phrase(new Chunk(pnamel + pnamet)));
                PdfTable.AddCell(pnameval);
 PdfPCell pggenderval = new PdfPCell(new Phrase(new Chunk(pggenderl + pggenderrad)));
                PdfTable.AddCell(pggenderval);


                pdfDoc.Add(PdfTable);
                pdfDoc.Close();
                Response.ContentType = "application/pdf";

                System.Web.HttpContext.Current.Response.Write(pdfDoc);

                Response.Flush();
                Response.End();

            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
In Design the link "Convert to PDF" can't worked.




Answers (7)