加载页面后下载文件

问题描述 投票:0回答:2

我有两个.aspx页面。在第一页中我有一个按钮,在其点击事件中,用户被重定向到第二页。在第二页的page_load事件中,我编写了下载文件的代码。

有用。但是当我在浏览器中完全加载第二页时,我需要下载这个文件(意思是,我能够看到第二页的所有内容)。

这是我的代码:

页1

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

第2页

protected void Page_Load(object sender, EventArgs e)
{
  // code to download file
}
asp.net c#-4.0 page-lifecycle
2个回答
1
投票

页面的LoadComplete事件发生在所有回发数据和视图状态数据加载到页面中之后,并且在为页面上的所有控件调用OnLoad方法之后。

用法示例(在C#代码中)

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
}

或者你可以使用以下jQuery函数

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

只有在完全加载页面时才会调用它。包括所有js,图像和其他资源。


0
投票

实现这一目标的两种方法:

ASP.NET方式 - 在“卸载”页面生命周期中写入文件下载代码。页面完全呈现到浏览器后触发卸载。页面刚开始加载时会激活Page_Load。

jQuery方式 - 在$ document.ready(){}内写一个asp.net方法来下载文件。 $ document.ready()在文档加载或文档准备好后执行。确保你在页面下面写了jquery方法。

© www.soinside.com 2019 - 2024. All rights reserved.