Redirect from pop up window - asp.net

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";
}

Related

Server side equivalent of "top.location.href"

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");

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.

ASP.NET postbacks lose the hash in the URL

On an ASP.NET page with a tabstrip, I'm using the hash code in the URL to keep track of what tab I'm on (using the BBQ jQuery plugin). For example:
http://mysite.com/foo/home#tab=budget
Unfortunately, I've just realized that there are a couple of places on the page where I'm using an old-fashioned ASP.NET postback to do stuff, and when the postback is complete, the hash is gone:
http://mysite.com/foo/home
... so I'm whisked away to a different tab. No good.
This is a webforms site (not MVC) using .NET 4.0. As you can see, though, I am using URL routing.
Is there a way to tell ASP.NET to keep the hash in the URL following a postback?
The problem is that the postback goes to the url of the current page, which is set in the action of the form on the page. By default this url is without #hash in asp.net, and its automatically set by asp.net, you have no control over it.
You could add the #hash to the forms action attribute with javascript:
document.getElementById("aspnetForm").action += location.hash
or, if updating an action with a hash already in it:
var form = document.getElementById("aspnetForm");
form.action = form.action.split('#')[0] + location.hash
just make sure you execute this code on window.load and you target the right ID
I tried to put the code from Willem's answer into a JS function that got called everytime a new tab was activated. This didn't work because it kept appending an additional #hash part to the URL every time I switched tabs.
My URL ended up looking like http://myurl.example.com/home#tab1#tab2#tab3#tab2 (etc.)
I modified the code slightly to remove any existing #hash component from the URL in the <form> element's action attribute, before appending on the new one. It also uses jQuery to find the element.
$('.nav-tabs a').on('shown', function (e) {
// ensure the browser URL properly reflects the active Tab
window.location.hash = e.target.hash;
// ensure ASP.NET postback comes back to correct tab
var aspnetForm = $('#aspnetForm')[0];
if (aspnetForm.action.indexOf('#') >= 0) {
aspnetForm.action = aspnetForm.action.substr(0, aspnetForm.action.indexOf('#'));
}
aspnetForm.action += e.target.hash;
});
Hope this helps someone!
I have another solution, implemented and tested with chrome, IE and safari.
I am using the "localStorage" object and it suppose to work all the browsers which support localStorage.
On the click event of tab, I am storing the currentTab value to local storage.
$(document).ready(function() {
jQuery('.ctabs .ctab-links a').on('click', function(e) {
var currentAttrValue = jQuery(this).attr('href');
localStorage["currentTab"] = currentAttrValue;
// Show/Hide Tabs
jQuery('.ctabs ' + currentAttrValue).show().siblings().hide();
// Change/remove current tab to active
jQuery(this).parent('li').addClass('active').siblings().removeClass('active');
e.preventDefault();
});
if (localStorage["currentTab"]) {
// Show/Hide Tabs
jQuery('.ctabs ' + localStorage["currentTab"]).show().siblings().hide();
// Change/remove current tab to active
jQuery('.ctabs .ctab-links a[href$="' + localStorage["currentTab"] + '"]').parent('li').addClass('active').siblings().removeClass('active');
}
});

jquery thickbox not working in 2005 ASP.NET

public void DisplayThickBox(Page page, int width, int height)
{
string script = "<script type='text/javascript'>";
script += "$(document).ready(function(){";
script += "tb.show('null', 'auto-insurance-redirect.aspx?keepThis=true&TB_iframe=true&height=" + height.ToString() + "&width=" + width.ToString() + "',null);";
script += "});";
script +="</script>";
ScriptManager.RegisterStartupScript(page, page.GetType(), "", script, true);
}
^^^Method to display the thickbox...^^^
DisplayThickBox(this, 518, 321);
^^^Call to method in the click event of the button that fires it...^^^
Page is just refreshing and the thickbox is never displayed. I'm trying to call the javascript manually since im doing some other stuff before i display the thickbox in the code behind...
Sounds like you're calling this on a button click event which would postback to the server. You'll need to register the javascript on the client and call it from javascript, not thru server side code. What you have now is a startup script which should run when the page loads, that's not really what you want.
Looking at your javascript parameters:
"tb.show('null', 'auto-insurance-redirect.aspx?keepThis=true&TB_iframe=true&height=" + height.ToString() + "&width=" + width.ToString() + "',null);"
I notice that some parameters are separated by & and others are separated by &. Maybe this is the problem.
Got it fixed, no idea how...I had tried so many things and everything was getting dirty and clustered, so I took everything out and started back from blank and it worked right off the bat.
Thanks everyone for their help!

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