?
Solved

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

Posted on 2009-04-27
11
Medium Priority
?
452 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:Shepwedd
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 24240665
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

0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24240667
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.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 7

Expert Comment

by:dineesh
ID: 24241198
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

0
 

Author Comment

by:Shepwedd
ID: 24241510
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?
0
 

Author Comment

by:Shepwedd
ID: 24241535
Carlnorrbom,

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

Expert Comment

by:carlnorrbom
ID: 24241708
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.
0
 

Author Comment

by:Shepwedd
ID: 24241855
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?
0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 24243361
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.
0
 

Author Comment

by:Shepwedd
ID: 24250441
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.
0
 

Accepted Solution

by:
Shepwedd earned 0 total points
ID: 24252175
I ended up using a hyperlink control which allowed me to specify a target for my url.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question