views:

35746

answers:

11

I want to do a Response.Redirect("MyPage.aspx") but have it open in a new browser window. I've done this before without using the JavaScript register script method. I just can't remember how =)

Thanks

+4  A: 

This is not possible with Response.Redirect as it happens on the server side and cannot direct your browser to take that action. What would be left in the initial window? A blank page?

John Sheehan
+10  A: 

Because Response.Redirect is initiated on the server you can't do it using that.

If you can write directly to the Response stream you could try something like:

response.write("<script>");
response.write("window.open('page.html','_blank')");
response.write("</script>");
KiwiBastard
This work, but then the page where my button is get's changes, its like the CSS or DIVS are being affected.
Etienne
A: 

You may want to use the Page.RegisterStartupScript to ensure that the javascript fires on page load.

CodeRot
+19  A: 

I just found the answer and it works :)

You need to add the following to your server side link/button:

OnClientClick="aspnetForm.target ='_blank';"

My entire button code looks something like:

<asp:LinkButton ID="myButton" runat="server" Text="Click Me!" OnClick="myButton_Click" OnClientClick="aspnetForm.target ='_blank';"/>

In the server side OnClick I do a Response.Redirect("MyPage.aspx"); and the page is opened in a new window.

The other part you need to add is to fix the form's target otherwise every link will open in a new window. To do so add the following in the header of your POPUP window.

<script type="text/javascript">
        function fixform() {
            if (opener.document.getElementById("aspnetForm").target != "_blank") return;

            opener.document.getElementById("aspnetForm").target = "";
            opener.document.getElementById("aspnetForm").action = opener.location.href;
            }
</script>

and

<body onload="fixform()">
does this work if Javascript is disabled?
Brian Boatright
Nope as this uses javascript to change the target of the form. Instead the page would submit as normal.
Toby Mills
Plus you could have a security violation if you want to redirect to a page outside your virtual directory.
Drejc
That may work, but it looks very brittle.
rick schott
Somehow in my case its not working. I'm opening the same page in the new window and writing the response to that page. But when I keep fixform() method in the masterpage it throws error saying document is null. Not sure why it is throwing still trying to find a solution. Though I've come up with a temporary solution by using onClientClick="aspnetForm.target='';" property for other buttons on that page.
JPReddy
A: 

You can use the window.open.

You best bet is to register a start up script, that will open you page.

Do not use Modal Window since it is only supported in IE.

David Basarab
+1  A: 

You can also use in code behind like this way

ClientScript.RegisterStartupScript(this.Page.GetType(), "",
  "window.open('page.aspx','Graph','height=400,width=500');", true);
+1  A: 

Thank you very much for this! I have been on google for most of the day and everyone says this is impossible to open a new window using a link button but THIS DOES WORK BRILLIANTLY!

Thank you!

Rich
A: 

Thanks Alot For Helps.

Hamidreza
A: 

The fixform trick is neat, BUT:

  1. You may not have access to the code of what loads in the new window.

  2. Even if you do, you are depending on the fact that it always loads, error free.

  3. And you are depending on the fact that the user won't click another button before the other page gets a chance to load and run fixform.

I would suggest doing this instead:

OnClientClick="aspnetForm.target ='_blank';setTimeout('fixform()', 500);"

And set up fixform on the same page, looking like this:

function fixform() { document.getElementById("aspnetForm").target = ''; }

tom
A: 

I always use this code... Use this code

String clientScriptName = "ButtonClickScript";
Type clientScriptType = this.GetType ();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager clientScript = Page.ClientScript;

// Check to see if the client script is already registered.
if (!clientScript.IsClientScriptBlockRegistered (clientScriptType, clientScriptName))
    {
     StringBuilder sb = new StringBuilder ();
     sb.Append ("<script type='text/javascript'>");
     sb.Append ("window.open(' " + url + "')"); //URL = where you want to redirect.
     sb.Append ("</script>");
     clientScript.RegisterClientScriptBlock (clientScriptType, clientScriptName, sb.ToString ());
     }
Abhishek Shrivastava
This code will never affect the CSS class so the parent window will not be affected at all!!
Abhishek Shrivastava
A: 

you can open new window from asp.net code behind using ajax like I did here http://alexandershapovalov.com/open-new-window-from-code-behind-in-aspnet-68/

protected void Page_Load(object sender, EventArgs e)
{
    Calendar1.SelectionChanged += CalendarSelectionChanged;
}

private void CalendarSelectionChanged(object sender, EventArgs e)
{
    DateTime selectedDate = ((Calendar) sender).SelectedDate;
    string url = "HistoryRates.aspx?date="
+ HttpUtility.UrlEncode(selectedDate.ToShortDateString());
    ScriptManager.RegisterClientScriptBlock(this, GetType(),
"rates" + selectedDate, "openWindow('" + url + "');", true);
}
Alexander Shapovalov