Response.Redirect() in new window in Asp.Net
Opening a new window in almost all browsers nowadays has to be invoked from a user click event. Almost all browsers (and this covers about 99% of cases) will block a popup that is invoked from a page load event, so I suggest you reconsider your solution, probably by showing a link that would open in new window.
What I would do, is open the new window on the click event, which would open your processing page, which in turn will redirect to whatever page you want. Basically, first open the popup with click event, and then do the redirect in your new page.
How to open the page in new window using response.redirect in c#
As Tom Gullen suggested that is the only way.
I am assuming you're using ASP.NET.
You can create a public function which receives the Url you want to open in the new windows.
This function then redirects to a page which does the dirty job.
Public Function ResponseRedirect(ByVal urlRedirect As String) As Boolean
HttpContext.Current.Response.Redirect("Redirector.aspx?goto=" & HttpUtility.UrlEncode(urlRedirect))
End Function
This is Redirector.aspx
Public Partial Class Redirector
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim OpenNewPage As String = ""
If Request.QueryString("goto") IsNot Nothing Then
Me.OpenNewPage = Request.QueryString("goto")
End If
If Request.UrlReferrer.AbsoluteUri IsNot Nothing Then
Me.Referer = Request.UrlReferrer.AbsoluteUri
End If
End Sub
Private _OpenNewPage As String = ""
Public Property OpenNewPage() As String
Get
Return _OpenNewPage
End Get
Set(ByVal value As String)
_OpenNewPage = value
End Set
End Property
Private _Referer As String = ""
Public Property Referer() As String
Get
Return _Referer
End Get
Set(ByVal value As String)
_Referer = value
End Set
End Property
End Class
and the HTML of Redirector.asp
<script type="text/javascript">
window.open('<%=Me.OpenNewPage%>', '_blank', '');
window.document.location.href = '<%=Me.Referer%>';
</script>
It's not really really elegant code but it does the trick.
Obviously, if javascript is disabled it doesn't work.
I've put together a sample here.
Hope it helps.
how to open page in new tab with response.redirect or response.write
try this:
missing +"
Response.Write("<script type='text/javascript'>");
Response.Write("window.open('DeliveryChallanPrint1.aspx?val="+this.txtPoNo.Text+"','_blank');");
Response.Write("</script>");
How do I use Target=_blank on a response.redirect?
You can't is the short answer. The browser is the only thing that can open up a new window.
What you can do is send a chunk of html down the response that has a link with your url as an href, target="_blank" and a chunk of javascript onload of the form that fakes a click. If this doesn't work then use a window.open(url);
response.write("<script>");
response.write("window.open('page.html','_blank')");
response.write("</script>");
Using Window.Open instead of Response.Redirect to open new window?
This code below ultimately does exactly what I needed it to:
<a href="<%= this.ResolveUrl("Search.aspx?id=" + lblGraphicNameValue.Text.Remove(lblGraphicNameValue.Text.Length -4)) %>"
target="_blank">Search Related</a>
This code does three things:
- 1) Opens Search in new page.
- 2) Truncates the search value by four
characters (I only needed part of the search string) - 3) Passes in
parameter to new page.
This accomplished exactly what I needed without resorting to custom classes or javascript, although it did make me have to use a link instead of a button.
Related Topics
How to Automatically Filter Out Soft Deleted Entities with Entity Framework
String.Split - by Multiple Character Delimiter
Wait Until File Is Completely Written
Is It Bad to Not Unregister Event Handlers
Why Implement Interface Explicitly
Serializing Private Member Data
Formatting Doubles for Output in C#
Checking for Null Before Event Dispatching... Thread Safe
How to Force Garbage Collector to Run
Round a Double to X Significant Figures
ASP.NET MVC 5 Group of Radio Buttons
How to Create/Edit a Manifest File
Unrecognized Escape Sequence for Path String Containing Backslashes
How to Run a Winform from Console Application