• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 810
  • Last Modified:

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?
0
weimha
Asked:
weimha
  • 4
  • 2
  • 2
  • +2
1 Solution
 
krunal_shahCommented:
Hi,
you can use,

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

in your code file.

Thanks,
Krunal
0
 
guru_samiCommented:
0
 
weimhaAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
guru_samiCommented:
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
 
hehdaddyCommented:
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
 
weimhaAuthor Commented:
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
 
codingbeaverCommented:
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
 
weimhaAuthor Commented:
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
 
codingbeaverCommented:
You didn't read my last post.
0
 
weimhaAuthor Commented:
Your right.  When I use the scriptmanager it works great.  Thank you.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now