weimha
asked on
C# ASP.NET redirect to new browser window
From server side code I need to come up with some way to open a new window navigating to a different url. I have server side code that has to run first to get the url. I tried Response.Write(<script>win dow.open(' http://testpage.aspx');</script >) and it works opening the new browser window, but the orignal page gets displayed wierd. The page is a web content form with a master page and it loses have of it's styles and content.
Is there a way to call a javascript function passing a url and have it open to a new window?
Is there a way to call a javascript function passing a url and have it open to a new window?
Inject JS code like shown here:
http://dotnetslackers.com/articles/aspnet/JavaScript_with_ASP_NET_2_0_Pages_Part1.aspx#s6
http://dotnetslackers.com/articles/aspnet/JavaScript_with_ASP_NET_2_0_Pages_Part1.aspx#s6
ASKER
I've tried the following two methods and it doesn't work. The Window never opens. What do I need to do to get the javascript to execute?
Also, this is coming from a web content form within a master page if that matters?
The code shown below is in the onclick event of the linkbutton.
Also, this is coming from a web content form within a master page if that matters?
The code shown below is in the onclick event of the linkbutton.
// 1st attempt
StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("window.opwn('");
sb.Append(NavigationManagement.System.ImagingViewer(imageId));
sb.Append("');<");
sb.Append("/script>");
Type t = this.GetType();
ClientScript.RegisterClientScriptBlock(t, "PopupScript", sb.ToString());
// 2nd attempt
Page.RegisterStartupScript("myscript", "<script>window.open('" + NavigationManagement.System.ImagingViewer(imageId) + "');</script>");
In first attempt did you try:
ClientScript.RegisterStart UpScript(t ,"PopupScr ipt", popupScript);
Also are there any popup blockers or make sure your browser has javascript enabled.
ClientScript.RegisterStart
Also are there any popup blockers or make sure your browser has javascript enabled.
You might want to download and add a JavaScript reference to the jQuery library (http://www.jquery.com) in your master page. Then adjust your script to be something like this:
string myScript = "<script type='text/javascript'>
$(document).ready(function ()
{
window.open('" + NavigationManagement.Syste m.ImagingV iewer(imag eId) + "');
});
</script>";
And then register it using RegisterStartupScript. This will ensure that the whole page/DOM is loaded before it executes your code. I had a developer with a similar problem yesterday and this fixed it.
string myScript = "<script type='text/javascript'>
$(document).ready(function
{
window.open('" + NavigationManagement.Syste
});
</script>";
And then register it using RegisterStartupScript. This will ensure that the whole page/DOM is loaded before it executes your code. I had a developer with a similar problem yesterday and this fixed it.
ASKER
I saw that I had a typo. I spelled open wrong. It works now using the RegisterClientScriptBlock approach, but it doesn't work inside an update panel. This logic is in a user control and this user contol is almost always within an update panel. I need it to work within an update panel.
Is this possible?
Is this possible?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I changed my code as shown below, but it still doesn't work if the control is within an update panel.
The update panel has UpdateMode="Always".
And we aware of the popup blocker issue. This application is an internal application and the user's want it in another window, so we're trying to do that.
The update panel has UpdateMode="Always".
And we aware of the popup blocker issue. This application is an internal application and the user's want it in another window, so we're trying to do that.
StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("window.open('");
sb.Append(NavigationManagement.System.ImagingViewer(imageId));
sb.Append("');<");
sb.Append("/script>");
Type t = this.GetType();
//this.Page.ClientScript.RegisterClientScriptBlock(t, "PopupScript", sb.ToString());
this.Page.RegisterStartupScript("PopupScript", sb.ToString());
You didn't read my last post.
ASKER
Your right. When I use the scriptmanager it works great. Thank you.
you can use,
Page.RegisterStartupScript
in your code file.
Thanks,
Krunal