2014-12-02 56 views
0

我有一个网格,使用iTextSharp DLL生成PDF文件的页面。该代码如下:生成PDF并创建下载后的绑定网格

var document = new Document(); 
          bool download = true; 
          if (download == true) 
          { 
           PdfWriter.GetInstance(document, Response.OutputStream); 
           BindGrid(); 
          } 
          string fileName = "PDF" + DateTime.Now.Ticks + ".pdf"; 
          try 
          { 

           document.Open(); 
           // adding contents to the pdf file.... 
          } 
          catch (Exception ex) 
          { 
           lblMessage.Text = ex.ToString(); 
          } 
          finally 
          { 
           document.Close(); 
           BindGrid(); 
          } 
          Response.ContentType = "application/pdf"; 
          Response.AddHeader("content-disposition", "attachment; filename=" + fileName); 
          Response.Flush(); 
          Response.End(); 
          BindGrid(); 
         } 

我需要一次下载窗口弹出,或者用户点击下载之后,在没有问题,我只是需要电网的用户生成后绑定到网格绑定pdf文件。我曾尝试结合上许多地方,你可以看到网格,但它们都没有工作,网格结合后,才刷新页面:(。

有什么办法,我可以做到这一点???

回答

0

结束到Response.End页面生命周期

我建议你:

  1. 生成PDF文件并将其保存在服务器上
  2. 绑定电网
  3. 冲洗生成的文件

事情是这样的:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
      BindGrid(); // Bind the grid here 

     // After reloading the page you flush the file 
     if (Session["FILE"] != null) 
      FlushFile(); 
    } 

    protected void gv_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     // Generate & Save PDF here 

     Session["FILE"] = fullFilePath; // The full path of the file you saved. 

     Response.Redirect(Request.RawUrl); // This reload the page 
    } 

    private void FlushFile() 
    { 
     string fullFilePath = Session["FILE"].ToString(); 
     Session["FILE"] = null; 

     // Flush file here 
    } 

希望这有助于。

干杯

+0

有些代码会有帮助:P – perkes456 2014-12-02 11:59:13

+0

检查编辑的答案。我没有测试它,但它应该工作。 – dario 2014-12-02 12:46:48