Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect users to another page in another window

Status
Not open for further replies.

JulesBos

Programmer
Sep 6, 2006
68
US
Hi,

I've got a button on a webform that redirects users to another page dependant on some variable values. However I don't want to redirect, I want to open the page in a new browser window. How is this possible?

Here's my code:

Code:
Private Sub PrintBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintBtn.Click
        Dim recordID As String = Me.CSDRecordIDLabel.Text
        Dim CSDNo As String = Me.CSDNoLabel.Text
        Response.Redirect("PrintCSD.aspx?CSDID=" + recordID + "&CSDNo=" + CSDNo)
    End Sub
 
You can use a link button or hyperlink and set the target = "_blank" or you can use javascript to open a popup window.
 
Hi, I've just tried this solution and I'm afraid it wasn't so obvious. A link button has an onclick event so I can use my code, but no target property. A hyperlink has a target property but no onclick event so I can't use my code. You just have to specify a URL, which is obviously dependant on variables.

Any suggestions?
 
Instead of response.redirect use something like this...
Code:
Page.RegisterStartupScript("OpenNewPage", "<script>" & _
                  "window.open('build your string here', '','600','800')" & _
                  "</script>")

J
 
Thanks for this, I've tried this:

Code:
 Dim recordID As String = Me.CSDRecordIDLabel.Text
            Dim CSDNo As String = Me.CSDNoLabel.Text
            Dim redirectPath As String = Global.linkPath
            Dim openPath As String = redirectPath + "CSDs/PrintCSD.aspx?CSDID=" + recordID + "&CSDNo=" + CSDNo
            Page.RegisterStartupScript("OpenNewPage", "<script>" & _
                  "window.open(openPath, '','600','800')" & _
                  "</script>")

and it doesn't do anything. What am I missing?

Julia
 
Ah, I think you have to set it to a variable, rather than just saying window.open. Here is exactly how I use it (I have an external js file for common functions...

In the .vb page
Code:
 Page.RegisterStartupScript("Open Report Viewer", "<script>" & _
                  "ShowResizableWindow('Reports/ReportsViewer.aspx', '','600','800')" & _
                  "</script>")

In the common function .js file
Code:
function ShowResizableWindow( url, userControlUniqueID, winHeight, winWidth, formName )
{
	wleft = (screen.width - winWidth) / 2;
	wtop = (screen.height - winHeight) / 2;
	
	//Jeremy Hurst - I added this to pass an optional form name, to open seperate windows
	if (formName == 'undefined')
	{
		formName = 'Form'
	}
	
	var windowFeatures = "width=" + winWidth + "px,height=" + winHeight + "px,top=" + wtop + ",left=" + wleft + ", resizable = 1";
	var win = window.open(url, formName, windowFeatures);
	win.focus();

	return true;
}
 
I'm pretty new to ASP.Net and don't know anything about Java. I'm afraid that one's gone way over my head! Do you know of any other solutions, or is this my only option?

Thanks again.
 
Sorry about that. Opening a new page is easy. Like mentioned above you have a couple of options. 1) You can use a hyperlink with a target=_blank, or 2) the javascript's window.open() method.

The (slightly) complicated part of what you are trying to do is that you want to open a pop-up after the page has been posted back. If you went with the hyperlink way then you would have to already have the redirection string built before postback (and therefore you wouldn't need to postback to build this). However, since you are building the string on the button_click event then you need some way to fire this new page off after the page is rendered again. So, you need to do it via JavaScript after building the string (JAVA and JavaScript are two different things, btw).

Take a look into the window.open() javascript method on google as that is what you need. If you want to implement the function that I'm using above then simply go to your .aspx markup and add it in the <head> like so...
Code:
<script language="javascript">
function... (just copy and paste)
</script>

Then in your code behind do what I did, except replace "Reports/ReportsViewer.aspx" with your string (or better yet, just a simple page like "
J
 
Thanks for taking the time to explain it to me! :) I appreciate it. As the hyperlink isn't an option (i.e. I can't use the variables), I'll have a go at implementing the javascript solution.

Julia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top