opening a html page on the client from server side - asp.net

On the server I receive xml from a webservice, I use xslt transformation on this xml to create a htm page. Now I need to show this htm page to the user by opening it in a new browser window. How do I achieve such functionality? My website is written in ASP.NET.
I have tried using
Response.Write("");
Response.Write("window.open('" + Server.MapPath("~/App_Data/HTMLPage.htm") + "','_blank')");
Response.Write("");
But this throws me an access denied error.
Thanks in advance.
Chandrasekhar

As I understood it, you want this new page to open in a new browser window, correct?
If so, you're going about it the wrong way. Response.Redirect will only redirect the current page, not instantiate a new browser window.
What you need to do is inject a JavaScript command into the page that opens a new browser page. That command is window.open. Here's a quick way to do it:
ClientScript.RegisterStartupScript(this.GetType(), "newpage", "window.open('" + address +"');", true);
This code will insert the JavaScript command to execute when the page reloads after submission. Note that address is the string variable that contains the address of the page you want to open.
Another very important note: most browsers will consider this a pop-up window and may very well block it.

Related

Creating multiple responses from one ASP page

When the user checks a checkbox and clicks a specific button, I already do some stuff on the server and then use Response.Redirect to "reset" the page. I also have a function which allows me to "export" a datatable to an excel spreadsheet by using a application/vnd.openxmlformats-officedocument.spreadsheetml.sheet response.
Both of these work fantastically when separate. But, what I now need to do is have both events take place after one button click.
It seems like I would need to Flush the response after exporting to excel and then redirect the response afterward, but I can't seem to be able to get this to work. I could be way off on this one, and need some assistance. Thanks
EDIT:
Alright, I have decided to go about this by opening a new page (downloadexcel.aspx) which will then initiate the download and close after the download is completed. I am opening downloadexcel.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('~/DownloadExcel.aspx', '', ''); </script>")
Response.Redirect("~/BulkImport.aspx")
If you want to do this in a single HTTP request then you will have a single response. You can either return HTTP 200 (with the file as the content) or HTTP 301 to redirect the client. You can't return both.
The bottom line is that you will need to rethink your approach, you could "clean" the form on the client side using javascript instead of reloading the page using a redirect.

How to close aspx page that is redirect from handler

I am in critical situation.
In my application I try to close opened window using javascript function.
function Close_Window() {
var myWin = window.open('', '_parent', '');
myWin.close();
}
this function is used in 3 aspx pages.
That page will open from mail means my application is used for sending Mail.
In that such as normal newsletter there will be three footer links like UnSubscribe,Forward A Friend,Change E-Mail address.
So on click of close button for above these three pages I had written that code.
The page will be opened from “ashx” file. And I try to close it using Javascript.
So close button is working when I direct click on the above three links.
But by right clicking on that I open it in new window or new tab close button is not working.
So please give me solution that how can I close aspx page using javascript or in code behind.
My purpose is page should be close any how by javascript or by code behind.
Note : I am opening that page using ashx handler.
If you want total control of opening and closing windows, keep the control and do not transfer the control to the browser. The behaviour of Html is well defined by W3C. The behaviour of new browser windows/tabs will vary between different browsers, and probably in the future.
Why do you open and close a page directly?
Probably you want to execute a GET request on the server to trigger some action. Take a look at the jQuery get method. So you can execute your request, without the need to open/close a new page.
If you want the user to make a choice, after clicking the link, I would work with a jQuery modal dialog. This pops up a dialog, but inside the current HTML page. Therefor you are in control of closing the dialog.
If you open a new window/tab the browser is in control, and the behaviour depends on the browser / user settings combination.

Unblock (jQuery BlockUI) after file sent to browser through response stream

I have an <asp:button /> that generates a PDF report. I want to use jQuery BlockUI to display a message that says: Generating Report..
Using: Response.BinaryWrite I am then sending the file to the user so it appears as a file download in the browser.
I cannot get $.unblockUI(); to fire. As soon as the file download begins or has completed or is cancelled I want it to dissappear. Currently it never does.. It's as though the page hasn't been re-loaded, I've hit the server but it comes back with the same page.
Things I have tried:
Setting a variable in JS to true so on document.ready() it will call a function if set to true. This doesn't work because even though I change the variable in code it doesn't change the HTML that is sent to the client.
This sort of thing: Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function() { $.unblockUI; }); //register with Microsoft way
$(document).ajaxStop($.unblockUI); //double the insurance and register with jquery way
never gets called..
Can this be achieved with an updatepanel?
Any thoughts?
Also in case it helps:
Response.AddHeader("Content-Disposition", "attachment;filename=""" & ShortFilename & """")
Response.AddHeader("Content-Length", byteArray.Length)
Response.BinaryWrite(byteArray)
Response.Flush()
Response.End()
I can see why this doesn't work sort of, the page is not refreshing in anyway there's just a response stream being sent to the browser, but surely there's an event I can latch on to?
An idea could be to create a child window that does the PDF loading and let the parent figure out when the child window has closed or something.
Is it possible for parent window to notice if child window has been closed?
The solution is to first block the UI as normal. Then make an AJAX request to your report generator, when the PDF is generated store it locally for a short time, or have it in a temporary folder that is cleared out when the user logs out or their login times out. Have the report generator return a success message and a URL.
Have the client ajax request handle the success message, remove BlockUI, then call the URL using:
window.location="http://yourURL/temp/report.pdf
The browser will start to download the file and you are done!
https://stackoverflow.com/a/7660817/181197

How can I programatically open a new page in a new tab from my codebind file in ASP.NET?

How can I programatically open a page in a new tab from my code behind file in ASP.NET after clicking on a button in my first page?
Hopefully, from the new page I could also get to the Session[] array.
Kelsey's code is correct, but is now depricated, the suggested way to do it now is to use the ScriptManager's methods like this.
ClientScript.RegisterStartupScript(GetType(), "SomeNameForThisScript",
"window.open('YourPage.aspx');", true);
"Code behind" runs on the server, no browser instances there to open/use.
Javascript runs in the browser, on the client's computer, it can open a new tab.
If you want, you will have to write a piece in C# that will generate a JavaScript snippet with the window.open Command.
Just register a window.open command in the start client script.
In your C# client side code (event):
RegisterStartupScript("SomeNameForThisScript", "window.open('YourPage.aspx');");
When you page is served up, the startup script will fire and open a new window. You can customize how the window.open works via attributes.
How about Response.Redirect("~/formname.aspx?Parameters=" + yourparamater); ?

Server-side detection that a page is shown inside an IFrame

Is it possible to determine - server-side - whether a page has been loaded within an IFrame?
When certain errors happen in my application the user gets redirected to Default.aspx and an error message is shown. The text of that error message gets set in session by exception handling code. The error message gets cleared from session once it has been shown.
However, part of my application has to use an IFrame (it's doing a 3D Secure card payment check, which mandates an IFrame in order to display the card provider's authentication UI). If an error takes place during this process my redirect takes effect within the IFrame. I am using JavaScript to detect this and reload Default.aspx correctly, but this means that I get two Page_Loads in rapid succession, and the error message only gets shown on the first one (and then cleared).
You can do it in client side: How to identify if a webpage is being loaded inside an iframe or directly into the browser window?
The workaround i found is put some identifier into querystring of a url opened inside iframe.
I don't think you can detect in the sense of having some sort of Page.IsInIFrame() kind of functionality, but you could consider having different base classes for those pages that are loaded in an IFrame and those that aren't so that you can know the error is from a request that was for an IFrame page that may help to some extent.
There's no way from the server-side. The only way is via javascript. When you do the redirect, can you pass the error message or code via a querystring?
Won't it work to redirect using Javascript with window.location? Forcing a full page redirect?
This is impossible because client can open iframe with javascript disabled. http://caniuse.com/#feat=iframe-sandbox
simply, check the url of current page..
if it's the same with the IFrame page then redirect to Default.aspx or whatever.
Dim urlpath1 As String = HttpContext.Current.Request.Url.AbsoluteUri
If Right(urlpath1, 13) = "WebForm1.aspx" Then
Response.Redirect("~/")
Else
Response.Write("It's OK!")
End If

Resources