Response.Redirect to new window Response.Redirect to new window asp.net asp.net

Response.Redirect to new window


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


You can use this as extension method

public static class ResponseHelper{     public static void Redirect(this HttpResponse response, string url, string target, string windowFeatures)     {         if ((String.IsNullOrEmpty(target) || target.Equals("_self", StringComparison.OrdinalIgnoreCase)) && String.IsNullOrEmpty(windowFeatures))         {             response.Redirect(url);         }         else         {             Page page = (Page)HttpContext.Current.Handler;             if (page == null)             {                 throw new InvalidOperationException("Cannot redirect to new window outside Page context.");             }             url = page.ResolveClientUrl(url);             string script;             if (!String.IsNullOrEmpty(windowFeatures))             {                 script = @"window.open(""{0}"", ""{1}"", ""{2}"");";             }             else             {                 script = @"window.open(""{0}"", ""{1}"");";             }            script = String.Format(script, url, target, windowFeatures);             ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true);         }     }}

With this you get nice override on the actual Response object

Response.Redirect(redirectURL, "_blank", "menubar=0,scrollbars=1,width=780,height=900,top=10");


Contruct your url via click event handler:

string strUrl = "/some/url/path" + myvar;

Then:

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "popup", "window.open('" + strUrl + "','_blank')", true);