user1690251
user1690251

Reputation: 13

Downloadinf a pdf in new window without redirecting

I have an aspx page which has several controls. When I click a button on this page, I need to get the values of the textbox and dropdowns and do some processing and pass it on to another aspx page. The second aspx will do further processing and generate a pdf to download. I don't want the user to be redirected to the second page. I simply want the pdf download to appear in a new window. How can I achieve this?

Upvotes: 0

Views: 883

Answers (1)

nunespascal
nunespascal

Reputation: 17724

You need to set the Content-Disposition http header and return the pdf directly.

Like this:

Response.ContentType = "application/x-download";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName));
Response.WriteFile(filePath + fileName);
Response.Flush();
Response.End();

Note: Don't set the Content-Disposition to application/pdf as this is recognized by many browsers and they will try to open it with the pdf-reader embedded in the browser.

How to post to second page on button click?

Set the PostbackUrl on your button click like this:

<asp:Button 
  ID="Button1" 
  PostBackUrl="~/TargetPage.aspx"
  runat="server"
  Text="Submit" />

You can read values from your current page on target page by accessing the controls using the PreviousPage property as follows:

TextBox SourceTextBox = 
    (TextBox)Page.PreviousPage.FindControl("TextBox1");
if (SourceTextBox != null)
{
    Label1.Text = SourceTextBox.Text;
}

Upvotes: 2

Related Questions