Link to home
Start Free TrialLog in
Avatar of weimha
weimhaFlag for United States of America

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>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?
Avatar of krunal_shah
krunal_shah

Hi,
you can use,

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

in your code file.

Thanks,
Krunal
Avatar of weimha

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.
 

// 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

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.
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.
Avatar of weimha

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?
ASKER CERTIFIED SOLUTION
Avatar of codingbeaver
codingbeaver
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of weimha

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.

          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

You didn't read my last post.
Avatar of weimha

ASKER

Your right.  When I use the scriptmanager it works great.  Thank you.