C# 生成pdf并创建下载后绑定网格
我有一个网格页面,它使用iTextSharp dll生成PDF文件。代码如下:C# 生成pdf并创建下载后绑定网格,c#,asp.net,pdf,itextsharp,C#,Asp.net,Pdf,Itextsharp,我有一个网格页面,它使用iTextSharp dll生成PDF文件。代码如下: var document = new Document(); bool download = true; if (download == true) { PdfWriter.GetIn
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文件后绑定网格。正如您所看到的,我已经尝试在许多地方绑定网格,但都没有成功,网格只有在我刷新页面后才会绑定:
有什么方法可以做到这一点吗?Response.End结束页面生命周期 我建议你: 生成PDF文件并将其保存在服务器上 绑定网格 刷新生成的文件 大概是这样的:
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
}
希望这有帮助
干杯检查编辑的答案。我没有测试它,但它应该可以工作。