Post Back not occuring after pdf is downloaded

江枫思渺然 提交于 2019-12-20 05:37:15

问题


I've developed a pdf file using itextsharp.

Pdf generation is working fine. After the pdf is created it's being downloaded.

My problem is when the user clicks on Generate PDF button , Pdf is generated and downloaded properly but postback doesn't occurs.

I want the postback to be occured because I want to Reset my Form after Pdf is generated that is Clear All Fields .

How Can I do this ?

Here is my code :

Method to Generate PDF :

  public void GetPDF(string quote_num)
    {
        string url = FilesPath.Path_SaveFile + Session["empcd"].ToString() +"-Quotation.pdf";
        Document disclaimer = new Document(PageSize.A4, 2, 2, 10, 10);
        PdfWriter writer = PdfWriter.GetInstance(disclaimer, new FileStream(url, FileMode.Create));
        writer.PageEvent = new myPDFpgHandler(quote_num);
        disclaimer.SetMargins(70, 10, 60, 80);    
        disclaimer.Open();
        GenerateQuotPDF getpdf = new GenerateQuotPDF();
        disclaimer = getpdf.GetPDFparams(disclaimer,quote_num, Session["empcd"].ToString(),txt_contactperson.Text,txt_contact1.Text,txt_company.Text,txt_address.Text,ddl_gene_desc.SelectedItem.ToString(),ddl_canopy.SelectedItem.ToString(),ddl_gene_type.SelectedItem.ToString(),txt_rentalamount.Text,txt_hours.Text,txt_variable.Text,ddl_terms.SelectedItem.ToString(),txt_remarks.Text,txt_technical.Text,ddl_sign1.SelectedValue,ddl_sign2.SelectedValue,txt_designation.Text,DateTime.Now);
        disclaimer.Close();

        System.IO.FileInfo file = new System.IO.FileInfo(url);
        if (file.Exists)
        {
            WebClient client = new WebClient();
            Byte[] buffer = client.DownloadData(url);
            Response.AddHeader("content-disposition", "attachment; filename=" + Session["empcd"].ToString() + "-Quotation.pdf");
            Response.AddHeader("content-length", buffer.Length.ToString());
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(buffer);
        }
    }

Generate PDF Button Code :

 protected void btn_submit_Click(object sender, EventArgs e)
    {
        if (IsValidQuotation())
        {
            string newQuotNum = rental_quotations.GetNewQuotNumber();
            rental_quotations.AddNewQuotation(newQuotNum, Session["empcd"].ToString(), ddl_gene_type.SelectedValue.ToString(), ddl_gene_desc.SelectedValue, ddl_canopy.SelectedValue, txt_company.Text, txt_address.Text, txt_contactperson.Text, txt_designation.Text, txt_contact1.Text, txt_contact2.Text, txt_rentalamount.Text, ddl_terms.SelectedValue, txt_hours.Text, txt_variable.Text, txt_remarks.Text,ddl_sign1.SelectedValue,ddl_sign2.SelectedValue,txt_technical.Text);
            GetPDF(newQuotNum);
            ClearAllFields();     //this is not working   

        }
    }

回答1:


Postback is occurring since the file is being created. Try the given solution. Your GetPDF(string quote_num) function is doing two tasks that you should break into two functions.

  1. Creating the pdf document.
  2. Downloading the pdf file after it is done.

Now, After you have created the document, you should clear the controls and then send the file as response. Therefore do it as follows:

Create pdf file.

public void CreatePDF(string quote_num)
{
    string url = FilesPath.Path_SaveFile + Session["empcd"].ToString() +"-Quotation.pdf";
    Document disclaimer = new Document(PageSize.A4, 2, 2, 10, 10);
    PdfWriter writer = PdfWriter.GetInstance(disclaimer, new FileStream(url, FileMode.Create));
    writer.PageEvent = new myPDFpgHandler(quote_num);
    disclaimer.SetMargins(70, 10, 60, 80);    
    disclaimer.Open();
    GenerateQuotPDF getpdf = new GenerateQuotPDF();
    disclaimer = getpdf.GetPDFparams(disclaimer,quote_num, Session["empcd"].ToString(),txt_contactperson.Text,txt_contact1.Text,txt_company.Text,txt_address.Text,ddl_gene_desc.SelectedItem.ToString(),ddl_canopy.SelectedItem.ToString(),ddl_gene_type.SelectedItem.ToString(),txt_rentalamount.Text,txt_hours.Text,txt_variable.Text,ddl_terms.SelectedItem.ToString(),txt_remarks.Text,txt_technical.Text,ddl_sign1.SelectedValue,ddl_sign2.SelectedValue,txt_designation.Text,DateTime.Now);
    disclaimer.Close();
}

Reset the controls.

ClearAllFields(); 

Send the file as response.

public void SendPDF(string url)
{
    System.IO.FileInfo file = new System.IO.FileInfo(url);
    if (file.Exists)
    {
        WebClient client = new WebClient();
        Byte[] buffer = client.DownloadData(url);
        Response.AddHeader("content-disposition", "attachment; filename=" + Session["empcd"].ToString() + "-Quotation.pdf");
        Response.AddHeader("content-length", buffer.Length.ToString());
        Response.ContentType = "application/pdf";
        Response.BinaryWrite(buffer);
        Response.End();
    }
}

Note that I also added Response.End() to clear the buffer.



来源:https://stackoverflow.com/questions/38006440/post-back-not-occuring-after-pdf-is-downloaded

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!