?
Solved

C# ASP.NET redirect to new browser window

Posted on 2010-01-12
10
Medium Priority
?
800 Views
Last Modified: 2013-12-17
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>window.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?
0
Comment
Question by:weimha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 10

Expert Comment

by:krunal_shah
ID: 26294658
Hi,
you can use,

Page.RegisterStartupScript("myscript","<script>window.open('http://testpage.aspx');</script>");

in your code file.

Thanks,
Krunal
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 26294670
0
 

Author Comment

by:weimha
ID: 26295178
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.
 

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

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 41

Expert Comment

by:guru_sami
ID: 26295351
In first attempt did you try:
ClientScript.RegisterStartUpScript(t,"PopupScript", popupScript);

Also are there any popup blockers or make sure your browser has javascript enabled.
0
 
LVL 6

Expert Comment

by:hehdaddy
ID: 26295471
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.System.ImagingViewer(imageId) + "');
    });
    </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.
0
 

Author Comment

by:weimha
ID: 26295897
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?
0
 
LVL 10

Accepted Solution

by:
codingbeaver earned 2000 total points
ID: 26296428
It is possible, but the way you are trying to do will fire browser's popup blocker, so your user may not be able to see the popup. With UpdatePanel, you can't use ClienScript, you should use this:
ScriptManager.RegisterStartUpScript(....)
But remember what I said about the popup blocker.
0
 

Author Comment

by:weimha
ID: 26303768
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.

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

Open in new window

0
 
LVL 10

Expert Comment

by:codingbeaver
ID: 26304014
You didn't read my last post.
0
 

Author Comment

by:weimha
ID: 26304268
Your right.  When I use the scriptmanager it works great.  Thank you.
0

Featured Post

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question