Link to home
Start Free TrialLog in
Avatar of Shepwedd
Shepwedd

asked on

How do I open my Response.Redirect in a new page?

I have the attached code but I can't seem to find information on how to open my link in a new page? Is it possible?

Thanks.
Response.Redirect("http://www.bbc.co.uk");

Open in new window

Avatar of carlnorrbom
carlnorrbom
Flag of Sweden image

Hi,

You can easily do it using client side script, please see the attached code sample.

/Carl.
Default7.aspx (markup):
 
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default7.aspx.vb" Inherits="Default7" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtUrl" runat="server" />
        <asp:Button ID="btnOpenWindow" runat="server" Text="Open Link" />
    </div>
    </form>
</body>
</html>
 
Default7.aspx.vb (code behind):
 
Partial Class Default7
    Inherits System.Web.UI.Page
 
    Protected Sub btnOpenWindow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpenWindow.Click
        If txtUrl.Text.Length > 0 Then
            Dim url As String = txtUrl.Text.Trim()
            ClientScript.RegisterStartupScript(Me.GetType(), "linkScript", "window.open('" & url & "');", True)
        End If
    End Sub
 
End Class

Open in new window

You can't do it with Response.Redirect(). Opening a new window is client side action and Response.Redirect has to be processed at the server.
Hi,

Try the following client side redirect..

regards
Dinesh

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>RedirectToSSL</title>
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name=ProgId content=VisualStudio.HTML>
<meta name=Originator content="Microsoft Visual Studio .NET 7.1">
</head>
<body MS_POSITIONING="GridLayout">
 
<script type="text/javascript"> 
<!-- 
 
var originalURL = window.location.host;
var replacementURL = originalURL.replace(originalURL, 'www.example.com'); 
 
window.location = 'http://'+ replacementURL ; 
 
--> 
</script>
 
</body>
 
</html>

Open in new window

Avatar of Shepwedd
Shepwedd

ASKER

Presently i'm using an imagebutton click event to launch the url and that's the way I would like to keep it. If using a client side script do I place your it anywhere within the body tag? Will the script still work in my scenario?
Carlnorrbom,

I'm coding in C# where I believe there is no "Handles" function, any work around?
Hi,

Change the code behind to:

partial class Default7 : System.Web.UI.Page
{
   
    protected void btnOpenWindow_Click(object sender, eventargs e)  {
        if (txtUrl.Text.Length > 0) {
            string url = txtUrl.Text.Trim();
            ClientScript.RegisterStartupScript(this.GetType(), "linkScript", "window.open('" + url + "');", true);
        }
    }
   
}

And in Your markup code:

<asp:Button ID="btnOpenWindow" runat="server" Text="Open Link" OnClick="btnOpenWindow_Click"/>

That should do it!

/Carl.
carlnorrbom,

I don't want a textbox (txtUrl) on my frontend, I only want the user to have to click an imagebutton to open up my link in a new window. Also, what namespace do I need to recognise ClientScript?
Hi,

In Your markup, delete the <asp:TextBox .../> row and change the button row to:

<asp:LinkButton ID="btnOpenWindow" runat="server" Text="Open Link" OnClick="btnOpenWindow_Click"/>

And in Your code behind, change to:

partial class Default7 : System.Web.UI.Page
{
    protected void btnOpenWindow_Click(object sender, eventargs e)  {
        ClientScript.RegisterStartupScript(this.GetType(), "linkScript", "window.open('http://www.bbc.co.uk');", true);
    }
}

The ClientScript() shorthand creates an instance of the System.Web.UI.ClientScriptManager object used to manage, register and add client side scripts to the page, hence it lives in the System.Web.UI namespace (.net 2.0 and onwards).

/Carl.
Can I link an image to a linkbutton? I need my link to be opened by clicking an image you see, hence the imagebutton control.
ASKER CERTIFIED SOLUTION
Avatar of Shepwedd
Shepwedd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial