Server side equivalent of "top.location.href" - asp.net

I'm in 'Home' page. From there 'Child' page is called in a radwindow. Now on a click event of 'Child' page, I want to close the radwindow and reload page 'Home' page.
Inorder to achieve this, on the click event of a button defined in 'Child' page. I am calling a JavaScript function:-
function ReloadHome
{
var abc = 'Home.aspx';
top.location.href = abc;
}
Question:- How to close radwindow and then navigate or reload 'Home' page, using a server side method. Not the javascript mentioned above.

You can try with
Close :
Response.Write("<script language='javascript'> { window.close();}</script>")
Redirect :
Response.Redirect("...Your url");

Related

Navigate to another page from radwindow

Scenario:-
I've a local asp.net website directory :- "http://localhost/DemoWebsite/". A "Secure" folder is inside it. Two webpages are inside that folder :- Page1.aspx and Page2.aspx. And one more webpage:- Page3.aspx is outside the folder.
Requirement:-
Now 'Page2.aspx' is opening in a RadWindow from the parent page 'Page1.aspx'. A button 'Cancel' is there in 'Page2.aspx' and on-click of that button I want to come out of RadWindow and directly move to 'Page3.aspx'.
Attempted Solution:-
Button's 'OnClientClick' event is set to a JS function 'LogOff()'.
Definition is:-
function LogOff()
{
top.location.href = "http://localhost/DemoWebsite/Page3.aspx";
}
The Problem is that since I can't give the above mentioned enitre link when I want this website to push in some other server. So if I give the link as:-
top.location.href = "~/Page3.aspx"; OR Just, top.location.href = "Page3.aspx";
And when I run the website and click the Cancel Button. It throws error "Resource can't be found." It is because after button click it tries the link "/DemoWebsite/Secure/Page3.aspx".
Question:- what to assign for top.location.hrefso that it tries the link "/DemoWebsite/Page3.aspx". As Page3.aspx is NOT in Secure folder.
You could do something like.... GetRadWindow().BrowserWindow.location.href = '../page3.aspx';
GetRadWindow() is a function that gets a reference to your radwindow....
function GetRadWindow()
{
var oWindow = null; if (window.radWindow)
oWindow = window.radWindow; else if (window.frameElement.radWindow)
oWindow = window.frameElement.radWindow; return oWindow;
}
The BrowserWindow part will get a reference to your window that the radwindow opened from. The rest just redirects the opening window to the location that you want.
EDIT: after looking at your question again i think you just need to know how to get to the relative path through javascript? if that is the case it is like this... document.location.href = '../'; //Up one level. The ../ will jump you back one level. If you just use /path then it will assume you are going from the same folder level that your current page is on.

catch close browser with onunload function using master page in ASP.NET

I have a website with master page. I want to catch a user trying to close the browser/tab. When I try using onunload in the body tag, it fires not only when I try closing the browser, but also when I navigate to another page.
Any idea how to only catch the event of closing the browser?
You can't distinguish closing the browser from navigating to another page. In both cases the current page is unloaded.
update: maybe you can handle some cases with some jquery, i.e. whenever a link is clicked, set some flag to be able to distinguish it from closing the window or entering a new URL:
<body onunload="checkForClose()">
...
<script>
var _isNavigation = false;
$(document).ready(function () {
// whenever a link is clicked set _isNavigation to true
$('a').click(function () {
_isNavigation = true;
});
});
function checkForClose() {
// show an alert if _isNavigation is not set
if (!_isNavigation) alert("closing the browser (maybe)");
}
</script>

ASP.NET/jQuery Is it possible to execute a link without opening a new page?

Is it possible to have a link, once clicked, execute the href target, but not pop up a new window?
I presume you mean using jQuery, since you have tagged it as such. Using an ajax call will hit the page and, if you wish, you could perform actions on finish. e.preventDefault will stop the click (visiting the url) to occuring.
Something like this?
$("a#mylink").click(function(e){
var goto = $(this).attr('href'); //get the url
$.ajax({
url: goto
}); //execute the href
e.preventDefault(); //prevent click
});
Ajax jQuery reference

Redirect from pop up window

how can i redirect to other page from pop up window with response.redirect.
I'm assuming that you're wanting to do this when the user presses a button or something on the popup. You can use ClientScript.RegisterStartupScript function to get ASP.Net to put some JavaScript in the right place so that it's executed as soon as the postback has completed. Something like this...
public void RedirectParent(string url)
{
string js = "";
js += "window.opener.location.href='" + url + "';";
js += "window.close();";
ClientScript.RegisterStartupScript(this.GetType, "redirect", js, true);
}
You can remove the window.close() line if you want to keep the popup open, but I'm guessing that you want to close it automatically.
The "redirect" bit on the last line is just the name that you want to assign to the script - this can be anything you wish.
Once popup is opened to a given URL you cannot use server side script to redirect the parent. You need to use javascript inside the popup:
if (opener && !opener.closed) {
opener.location.href = "http://www.example.com/somenewurl";
}

Detect F5 being pressed and Refresh

I have a webform and i want to detect if F5 button was pressed or if the page was refreshed. I know about postback but it is not what i'm looking for. I have a gridview that loads in a modal popup when a button is clicked and a parameter's value is set for the gridview. When refresh is hit and if the modal popup button was previously clicked the modal popup is visible right after refresh. I want to detect if the page is refreshed to prevent this. any ideas? I thought to try Override but I'm not exactly sure how to use it. I tried Control.ModifierKeys but I don't have access to ModifierKeys.
Pressing F5 or physically clicking the browser refresh behaves similarly to navigating away from the page. This is captured in the event window.onunload. Try the snippet example below:
window.onbeforeunload = function (evt) {
var message = 'Are you sure you want to leave?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
This will capture the refresh and allow you to do something or prompt the user.
Reemember that hotkeys are processed in the client side in the browser. The easiest way to implement this is through javascript.
Look at the following link:
http://snippets.dzone.com/posts/show/3552

Resources