window.open and window.location in the same script - asp.net

I have a small problem, I have a linkbutton that I am linking to a public sub that does the following:
Dim url As String = "articledownload.aspx?articleID=" & LatestNewsLetter.ArticleID
Page.ClientScript.RegisterStartupScript(Me.GetType(), "OpenWin", "<script>window.open('" & url & "'); window.location('newsReleases.aspx')</script>")
This works with IE, but not chrome. I have tried switching the order of the scripts around but with the same results. I am trying to open a new window with a file download and at the same time redirect the current page to another location. Is there another method of accomplishing this?

Use
window.location.href = 'newsReleases.aspx';
In order to do the second bit of redirecting the main page.
JSFiddle.

Related

ASP.NET "application/pdf": code runs only once

I'm working on a SharePoint 2013 site and I've added the ability to save pages in PDF. The PDF conversion is handled by the third party library SelectPdf.
I managed to get everything to work (rendering and file download), except that the "PDF Download" button that I have on my page works only 1 time. Meaning, the click event on the code behind is fired only once, no matter how many times I click the button (notice that I click it with intervals of 10+ seconds). If I want to download the PDF file again, I have to refresh the page.
I put together a "hello world" example (see below) in order to pinpoint the problem:
protected void lnkPdfDownload_Click(object sender, EventArgs e)
{
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=test.pdf");
/************************************ Create PDF File ************************************/
string html = #"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"">
<html dir=""ltr"" lang=""en-US"">
<body><h1>Hello World</h1></body>
</html>";
HtmlToPdf converter = new HtmlToPdf();
PdfDocument doc = converter.ConvertHtmlString(html);
byte[] bytes = doc.Save();
Response.OutputStream.Write(bytes, 0, bytes.Length); // ALTERNATIVE: doc.Save(Response.OutputStream);
/************************************ Create PDF File ************************************/
//Response.End(); // This throw a ThreadAbortException, therefore I'm using the alternative code below
Response.Flush();
Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
At the beginning I thought it was Response.End() that caused the issue (by throwing the ThreadAbortException), but I replaced it with other code and I still have the same problem (no exceptions are thrown now).
I don't think the problem is in the SelectPdf library: I can comment out the entire block (between the "Create PDF File" comments), and I still get the same thing (obviously no PDF is generated).
I noticed that, at most, I can successfully click the "download" button up to 2 times (it's rare, and not consistent): the third time nothing happens.
While this isn't a huge deal, I think there is something wrong going on that I'm not seeing. Here is why: after I click the "download" button (and get my PDF file), I am not able to go on edit mode in my SharePoint page. The "loading" message keeps spinning but nothing happens (again, unless I refresh the page).
Has anyone had this problem? I looked online but I couldn't find anything about it.
I'm using Internet Explorer 11 and Chrome 51. Please let me know if you need more information. Thank you.
Are you sure there are not javascript/jquery errors happening when the download button is clicked that prevent the re-clicking of the PDF button and also going into edit mode?
Especially since refreshing the page makes everything work again.

Selenium iframe selection issue

I'm testing our website login sequence with selenium. When I click login it loads an iframe, and the iframe id value changes each time it is loaded. So the test case fails in selenium.
What is the the command to read only the dynamic part of the iframe and locate it during the test run?
try this for capturing the popup,
String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Iterator<String> windowIterator = browser.getWindowHandles();
while(windowIterator.hasNext()) {
String windowHandle = windowIterator.next();
popup = browser.switchTo().window(windowHandle);
}
Hope it will helpful to you
Use FrameIndex to switch iframes.
driver.switchTo().frame(1);
or use xpath
driver.switchTo().frame(webElement)
Or share Html Code so we can give a better solution.

Open a new page using HTML Response/JS ASP

Alright, I have decided to go about this by opening a new page (newPage.aspx) which will then initiate the download and close after the download is completed. I am opening newPage.aspx using javascript by writing it to the response of the current page. For some reason, however, the window is not being opened before the original page is re-directed. Is their a method to be called before I redirect? Maybe I have a syntax issue?
Response.Write("<script type='text/javascript'>window.open('~/newPage.aspx', '', ''); </script>")
Response.Redirect("~/oldPage.aspx")
EDIT:
I tried the following but it did not work... (I am working in an Update Panel)
ScriptManager.RegisterStartupScript(udpMain, udpMain.GetType(), "openExcel", "window.open('~/newPage.aspx', '' , '');", True)
EDIT 2: So Close
Ok, so this only works if I comment out the Response.Redirect. It seems that the RegisterStartupScript method takes place on the page load after the Response.Redirect method. Is there any known fix for this?
ScriptManager.RegisterStartupScript(udpMain, udpMain.GetType(), "openExcel", "window.open(NewPage.aspx'); location.href='OldPage.aspx';", True)
Response.Redirect("~/OldPage.aspx")
You can redirect via client script as well. By the way when using ASP.NET preferable way of embedding JS into HTML is ClientScript.RegisterStartupScript e.g
ClientScript.RegisterStartupScript(Me.GetType(), "JSCode" & Rnd(1).ToString, "window.open('newPage.aspx'); location.href = 'oldPage.aspx'", True)

Web form non-responsive after download

I'm working in ASP.NET (2.0) and we have a page where a user is able to select and download a series of files as a zip. I got this to work without undo difficulty by using the DotNetZip library (which is probably not relevant to the problem, but included for completeness.)
After the user checks which files they want to download, the page does a postback, and in the button click event handler, I use the following code:
Response.Clear();
Response.BufferOutput = false; // false = stream immediately
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=FileRequest.zip");
using (ZipFile zip = new ZipFile())
{
zip.AddFile(MapPath("/datafiles/fileToZip.pdf"), "");
zip.Save(Response.OutputStream);
}
Response.Close();
And this all seems to work great. The user clicks the button, a download window pops up, the user downloads the zip. All is good...
...until they decide they want to do something else on the page. The buttons in the form are no longer responsive. For instance, if I click the download button again, it does nothing. If I reload the page, I can repeat this behavior...it works once, then does nothing.
I'm not understanding why the browser doesn't send the new request. It's not "spinning" or otherwise acting busy. I thought that this might be a browser issue, but I've repeated it in both IE and Firefox, so it seems likely that it's something I'm not understanding. Strangely, it's only form submission elements that seem to be non-responsive. Javascript still works, and so do regular links.
So why is this happening, and how do I get around it?
The problem is likely down to you returning;
Response.ContentType = "application/zip";
Once this has been sent (along with the actual content), the browser would assume it has nothing left to do (I believe).
You probably should create a new window specifically for downloading the files by saving the file selection in a session parameter (or equivalent) and opening a popup window that has your download code in.
This will leave the parent page in a suitable state.
Content-disposition header seems to be a discouraged solution. But the same effect of ASP.NET forms not being responsive occurs if you use standard Redirect to a zip file.
I solved very similar problem (returning *.csv files by the server for download) using Tančev Saša's code in "Response.Redirect to new window" Q&A and it works great. Perhaps it might produce some popup warnings in some browsers, but I think this is how it's often done in download sites.

Generating pdf before leaving a page in Asp.net

After submitting a form and before Redirecting a page need to generate a pdf, ask the user to save the file, then redirect the page .
Below is my code
DataSet objData = objInsert.Execute();
DocLib dl = new DocLib();
System.IO.MemoryStream ms = dl.GenerateForm(objData.Tables[0].Rows[0]);
Response.AddHeader("content-disposition", "attachment;filename=" +OrderNo + ".pdf");
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.Redirect("OrderList.aspx");
But currently once the save dialog box appears the page stops navigation and remains in the same page. How to redirect to another page.
I can't see a reason to effect Redirect's work. As another solution, you can try this.
Page.ClientScript.RegisterStartupScript(typeof(Page),"navigation",
"<script>window.location.href='OrderList.aspx'</script>")

Resources