How to open the page in new window using response.redirect in c#
Can anybody suggest how we can open the page in new window using Response.Redirect
in c#, I know we can do it using response.write as given below:
Response.Write("<script>");
Response.Write("window.open('"+url+"','_blank')");
Response.Write("</script>");
But what when javascript is disabled, the above code will not run.
Plea开发者_如何转开发se suggest!!
Response.redirect can't open a new window, it redirects the clients HTTP request to a new location in the current browser window. This is a limitation of server side, to perform a popup on the client machine you will at some point require client side code (IE, Javascript).
If you can settle for using a ASP:Hyperlink rather than a ASP:Linkbutton or ASP:Button you can handle the navigation a bit easier. Rather than using Response.Redirect during a postback, just build the hyperlink's NavigateURL during Page_Load or some other event:
protected void Page_Load (object sender, EventArgs e)
{
lnkViewPage.NavigateURL = sURL;
lnkViewPage.Target = "_blank";
}
Of course it is more polite to leave .Target alone because unlike buttons, linkbuttons, and imagebuttons, hyperlinks can be right clicked and "open in new page/tab" would be available from the context menu, giving the user control of how to handle the navigation.
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.
There is no way to dynamically open a new window without javascript.
If you want the window to always redirect you can pass back HTML that tells the browser to redirect in the header information. Like so:
<META HTTP-EQUIV="Refresh" CONTENT="5; URL=html-redirect.html">
Remember this will basically happen right away (or in the case above in 5 seconds). If you want something to cause the page to go somewhere else without javascript you need a link the user clicks on.
You can add this dynamically in C# like this:
Dim reMetaTag As New HtmlMeta
reMetaTag.HttpEquiv = "Refresh"
reMetaTag.Content = "5;URL=http://www.site.com"
Page.Header.Controls.Add(reMetaTag)
If you want to disable javascript and still be able to open a window i suggest you to become a magician.
Using javascript and response.redirect for a server button(for example) without refreshing parent window you should use the following code:
<input type="button" id="Example" value="Click!" runat="server"
onclick="var win = window.open('','Stack','width=500,height=600,menubar=no');
document.forms[0].target = 'Stack';" onserverclick="Save" />
Save function defined in your page behind code would be something like this
protected void Save(object sender, EventArgs e)
{
//your code here
Response.Redirect(url);//url it's a string for target url
}
So Tom Gullen was wrong and Manu's idea would give you a headache because it refreshes parent page.
Hope this helps somebody. :)
if you are using http then use below code
Response.Write("<script>window.open ('URL','_blank');</script>");
this code cannot be used for https for https do below
javascript in page
function shwwindow(myurl) {
window.open(myurl, '_blank');
}
in c# code behind
string URL = ResolveClientUrl("~") + "**internal page path**";
ScriptManager.RegisterStartupScript(this, this.GetType(), "show window",
"shwwindow('"+URL+"');", true);
this code cannot bybass the browser popup blocker. user must allow it to run. for ie it opens in new window or new tab up to ie version in firefox and chrome it opens new tab
enjoy it!!
精彩评论