如何从另一页面后面的代码更新一页上的隐藏字段值?

时间:2022-08-25 21:49:15

I have a page, behaviorAnalysis.aspx, that is calling a javascript that does two things: 1) Display a modal dialog with a please wait message; and 2) creating an iFrame and calling a second page, behaviorAnalysisDownload.aspx via jQuery:

我有一个页面,behaviorAnalysis.aspx,它调用一个javascript做两件事:1)显示一个带有请等待消息的模态对话框; 2)通过jQuery创建一个iFrame并调用第二个页面,behaviorAnalysisDownload.aspx:

function isMultiPageExport(exportMedia) {
    waitingDialog.show("Building File<br/>...this could take a minute", { dialogSize: "sm", progressType: "warning" });

    var downloadFrame = document.createElement("IFRAME");

    if (downloadFrame != null) {
        downloadFrame.setAttribute("src", 'behaviorExport.aspx?exportType=html&exportMedia=' + exportMedia);
        downloadFrame.style.width = "0px";
        downloadFrame.style.height = "0px";
        document.body.appendChild(downloadFrame);
    }
}

The second page is downloading an Excel file using the following code snippet:

第二页是使用以下代码片段下载Excel文件:

//*****************************************
//* Workbook Download & Cleanup
//*****************************************
MemoryStream stream = new MemoryStream();
wb.Write(stream);
stream.Dispose();

var xlsBytes = stream.ToArray();
string filename = "Behavior Stats YTD.xlsx";

MemoryStream newStream = new MemoryStream(xlsBytes);

if (Page.PreviousPage != null)
{
    HiddenField exp = (HiddenField)Page.PreviousPage.FindControl("hidDownloadStatus");
    exp.Value = "Complete";
}

HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename);
HttpContext.Current.Response.BinaryWrite(xlsBytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();

As you can see, I was hoping to update a hidden field on the calling page before pushing the download through; however, PreviousPage is null. Is there another approach I can use, to update the hidden field value from the calling page, behaviorAnalysis.aspx?

如您所见,我希望在推送下载之前更新呼叫页面上的隐藏字段;但是,PreviousPage为null。我可以使用另一种方法来更新调用页面中的隐藏字段值behaviorAnalysis.aspx吗?

3 个解决方案

#1


0  

I would first recommend this jQuery library, which does exactly what you want with the background download and modal, but it's been tested cross-browser and has a lot of neato features already available: http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/

我首先会推荐这个jQuery库,它可以完全按照你的需要进行后台下载和模态,但它已经过跨浏览器测试,并且已经有很多neato功能可供使用:http://johnculviner.com/jquery-file-下载插件换AJAX样功能丰富,文件下载/

If you don't like that, you can take a similar approach to what that plugin does. Instead of trying to set the value of a HiddenField on the parent, just add a cookie:

如果您不喜欢这样,您可以采用类似于该插件的方法。不要尝试在父级上设置HiddenField的值,只需添加一个cookie:

Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" });

After your first page appends the iFrame, just use setInterval() to check to see if that cookie exists with something like:

在您的第一个页面附加iFrame后,只需使用setInterval()检查该cookie是否存在,例如:

if (document.cookie.indexOf('fileDownload') > -1) {
    document.cookie = 'fileDownload=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;' // remove cookie
    // it was a success, so do something here
}

Of course, you'll want to put some sort of a timeout or logic to handle errors, but that should cover the basics of it.

当然,你需要设置某种超时或逻辑来处理错误,但这应该涵盖它的基础知识。

#2


0  

Is there any reason why you have to offer the file on the cliend side by creating an Iframe? May you can call directly your server side method on the same page.

你有什么理由要通过创建一个iframe来提供cliend方面的文件吗?您可以在同一页面上直接调用服务器端方法。

Another possibility would be to subscripe the iframe onload event and setting there the hidden value (client side).

另一种可能性是订阅iframe onload事件并在那里设置隐藏值(客户端)。

'<iframe src="....." onload="downloadComplete()"></iframe>';

#3


0  

In second Page add this:-

在第二页添加: -

<%@ PreviousPageType VirtualPath ="~/PreviousPage.aspx" %>

<%@ PreviousPageType VirtualPath =“〜/ PreviousPage.aspx”%>

Then try and let me know if this works.

然后尝试让我知道这是否有效。

#1


0  

I would first recommend this jQuery library, which does exactly what you want with the background download and modal, but it's been tested cross-browser and has a lot of neato features already available: http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/

我首先会推荐这个jQuery库,它可以完全按照你的需要进行后台下载和模态,但它已经过跨浏览器测试,并且已经有很多neato功能可供使用:http://johnculviner.com/jquery-file-下载插件换AJAX样功能丰富,文件下载/

If you don't like that, you can take a similar approach to what that plugin does. Instead of trying to set the value of a HiddenField on the parent, just add a cookie:

如果您不喜欢这样,您可以采用类似于该插件的方法。不要尝试在父级上设置HiddenField的值,只需添加一个cookie:

Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" });

After your first page appends the iFrame, just use setInterval() to check to see if that cookie exists with something like:

在您的第一个页面附加iFrame后,只需使用setInterval()检查该cookie是否存在,例如:

if (document.cookie.indexOf('fileDownload') > -1) {
    document.cookie = 'fileDownload=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;' // remove cookie
    // it was a success, so do something here
}

Of course, you'll want to put some sort of a timeout or logic to handle errors, but that should cover the basics of it.

当然,你需要设置某种超时或逻辑来处理错误,但这应该涵盖它的基础知识。

#2


0  

Is there any reason why you have to offer the file on the cliend side by creating an Iframe? May you can call directly your server side method on the same page.

你有什么理由要通过创建一个iframe来提供cliend方面的文件吗?您可以在同一页面上直接调用服务器端方法。

Another possibility would be to subscripe the iframe onload event and setting there the hidden value (client side).

另一种可能性是订阅iframe onload事件并在那里设置隐藏值(客户端)。

'<iframe src="....." onload="downloadComplete()"></iframe>';

#3


0  

In second Page add this:-

在第二页添加: -

<%@ PreviousPageType VirtualPath ="~/PreviousPage.aspx" %>

<%@ PreviousPageType VirtualPath =“〜/ PreviousPage.aspx”%>

Then try and let me know if this works.

然后尝试让我知道这是否有效。