Aijaz Chauhan
Aijaz Chauhan

Reputation: 1649

Download a file after page is loaded

I have two .aspx pages. In the first page I have a button, on its click event the user is redirected to a second page. In the page_load event of the second page, I wrote code to download a file.

It works. But I need to download this file when the second page is completely loaded in the browser (meaning, I'm able to see all the content of the second page).

Here is my code:

Page-1

protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
{
   Response.Redirect("Page-2.aspx");
}

page-2

protected void Page_Load(object sender, EventArgs e)
{
  // code to download file
}

Upvotes: 0

Views: 2612

Answers (2)

Pawan
Pawan

Reputation: 1075

The LoadComplete event of page occurs after all postback data and view-state data is loaded into the page and after the OnLoad method has been called for all controls on the page.

Example usage (in your C# code)

protected void Page_Load(object sender, EventArgs e)
{
      Page.LoadComplete +=new EventHandler(Page_LoadComplete);
}

void  Page_LoadComplete(object sender, EventArgs e)
{
    // call your download function
}

Alternately you can use following jQuery function

$(document).ready(function() 
{
    //page is fully loaded and ready, do stuff here
}

it will be called only when page is loaded fully. Including all js, images and other resources.

Upvotes: 1

Newton Sheikh
Newton Sheikh

Reputation: 1396

Two ways to achieve this:

ASP.NET way -- write the file download code on "Unload" page life cycle. Unload is fired after the page has fully been rendered into the browser. Page_Load fires when the page has just started loading.

jQuery way -- inside $document.ready(){} write a call to asp.net method to download the file. $document.ready() gets executed after the your document has loaded or the document is ready. make sure u write the jquery method below the page.

Upvotes: 0

Related Questions