In the master page of all my web form pages I've set up an overlay which blocks user interation whenever form submitted until browser renders next page response:
<script type="text/javascript">
function overlayWhenSubmit() {
document.getElementById('spinnerOverlay').style.display = 'block';
}
//spinnerOverlay has position:fixed and top/bottom/left/right:0
</script>
protected override void OnPreRender(EventArgs e) {
this.ClientScript.RegisterOnSubmitStatement(
typeof(Page), null, "overlayWhenSubmit();"
);
base.OnPreRender(e);
}
This works great until I try to provide file download:
public static void DownloadFile(this System.Web.HttpResponse @this, string physicalFilePath) {
@this.ContentType = "application/octet-stream";
@this.AppendHeader("Content-Disposition", $"Attachment; filename=\"{Path.GetFileName(physicalFilePath)}\"");
@this.TransmitFile(physicalFilePath);
}
// ... then in some event handler in my page:
Response.DownloadFile(thePathOfFileToDownload);
The file downloads OK, but the original page keeps being overlaid and therefore unusable.
Is there a way the page can get notified when the submitted request has finished so it can turn off the overlay?
CodePudding user response:
The closest thing so far is try not to popup the overlay for the control which leads to non-HTML response:
<script type="text/javascript">
function overlayWhenSubmit() {
if (!document.getElementById('noOverlayWhenSubmit').value)
document.getElementById('spinnerOverlay').style.display = 'block';
document.getElementById('noOverlayWhenSubmit').value = '';
}
function noOverlayWhenSubmit() {
document.getElementById('noOverlayWhenSubmit').value = true;
}
</script>
<input type="hidden" id="noOverlayWhenSubmit" value="" />
<asp:Button runat="server" OnClientClick="noOverlayWhenSubmit()"></asp:Button>