Clear all fields after the pdf is generated [dupli

2019-09-05 13:28发布

问题:

This question already has an answer here:

  • Post Back not occuring after pdf is downloaded 1 answer

I have a Webform which have some textboxes and dropdowns .

After clicking on Save button the data is saved in database and the pdf is generated using that data .

After the generation of pdf , the pdf file is opened in browser .

I've generated pdf file using a method then after i am calling a method to clear all fields .

After the pdf is generated , the user could press back button and can go back to previous page , Due to this reason I want all fields to be cleared but its not working . How can I resolve this ?

Here is my Code :

Save Button Code :

 protected void btn_submit_Click(object sender, EventArgs e)
    {
        if (IsValidQuotation())
        {
            string newQuotNum = rental_quotations.GetNewQuotNumber();
            double max_load = (Convert.ToDouble(generator_category.GetGeneratorDesc(ddl_gene_desc.SelectedValue)) * 0.8) * ((double)80 / (double)100);
            double min_load = (Convert.ToDouble(generator_category.GetGeneratorDesc(ddl_gene_desc.SelectedValue)) * 0.8) * ((double)50 / (double)100);
            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, min_load.ToString(), max_load.ToString(),ddl_sign1.SelectedValue,ddl_sign2.SelectedValue);
            GetPDF(newQuotNum);
        }
    }

This is the method where PDF is generated :

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(10, 10, 70,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();
        ClearAllFields(); // this is not working in back Case 
        System.IO.FileInfo file = new System.IO.FileInfo(url);
        if (file.Exists)
        {
            WebClient client = new WebClient();
            Byte[] buffer = client.DownloadData(url);
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", buffer.Length.ToString());
            Response.BinaryWrite(buffer);
        }
    }

After the pdf is opened in the browser , the user could go back to that page and all fields are not cleared

How can I Handle this ???

Thanks for your Help

回答1:

you can achieve by either using the enableviewstate='false' of the page

OR

you can reset all the control value after the execution of 'GetPDF(newQuotNum)' function call. Like this

protected void btn_submit_Click(object sender, EventArgs e)
{
    if (IsValidQuotation())
    {
        string newQuotNum = rental_quotations.GetNewQuotNumber();
        double max_load = (Convert.ToDouble(generator_category.GetGeneratorDesc(ddl_gene_desc.SelectedValue)) * 0.8) * ((double)80 / (double)100);
        double min_load = (Convert.ToDouble(generator_category.GetGeneratorDesc(ddl_gene_desc.SelectedValue)) * 0.8) * ((double)50 / (double)100);
        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, min_load.ToString(), max_load.ToString(),ddl_sign1.SelectedValue,ddl_sign2.SelectedValue);
        GetPDF(newQuotNum);
        ClearForm();
    }
}

private void ClearForm()
{
    foreach( var control in this.Controls )
    {
         var textbox = control as TextBox;
         if (textbox != null)
              textbox.Text = string.Empty;

         var dropDownList = control as DropDownList;
         if (dropDownList != null)
              dropDownList.SelectedIndex = 0;

         }
 }

hope this will resolve your problem.



标签: c# asp.net itext