Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 How difficult can it be?

Author  Topic 

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2005-12-07 : 12:00:10
I'm trying to do something that I feel should be very simple but can't find a simple answer!

I have an ASP.net page that has a text box and a button. The user is to put some text in the box and click the button (told you it was simple )

What I then want is for a NEW browser window to open and show a URL that is partially dependent on the contents of the text box. I know about response.redirect but that opens in the same window.

Is there not some simple .net code that can do this? Do I need to delve into Javascript?

thanks

steve

-----------

Facts are meaningless. You could use facts to prove anything that's even remotely true!

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-12-07 : 12:15:01
The key is the "target" attribute of the form element on that page. Set target="_blank" and post backs will open up in a new window; from there, you can redirect to wherever you need.

For example:


<form id="Form1" method="post" runat="server" target="_blank">
<asp:TextBox id="TextBox1" runat="server/>
<asp:Button id="Button1" runat="server" Text="Search Google"/>
</form>


and in the code-behind:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("http://www.google.com/search?q=" & Server.UrlEncode(TextBox1.Text))
End Sub
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2005-12-07 : 12:15:18
You need to use javascript to do that. You need to use the window.open function.


if(document.getElementById)
window.onload = OnLoad;

function OnLoad()
{
if(document.getElementById("<The ID of your button>") != null)
document.getElementById("<The ID of your button>").click = openNewWindow;
}

function openNewWindow()
{
window.open("http://someplace/somepage.aspx?TextBox=" + document.getElementById("<The ID of your textbox>").value);
return false;
}


Dustin Michaels
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2005-12-07 : 12:17:26
quote:
Originally posted by jsmith8858

The key is the "target" attribute of the form element on that page. Set target="_blank" and post backs will open up in a new window; from there, you can redirect to wherever you need.

For example:


<form id="Form1" method="post" runat="server" target="_blank">
<asp:TextBox id="TextBox1" runat="server/>
<asp:Button id="Button1" runat="server" Text="Search Google"/> </form>


and in the code-behind:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("http://www.google.com/search?q=" & Server.UrlEncode(TextBox1.Text))
End Sub




Thats a good solution, it will work even when javascript is disabled in the browser.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-12-07 : 12:18:52
Also it allows the SERVER to process the textbox and decide which URL to open in the new window, as opposed to having the client do it.
Go to Top of Page

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2005-12-08 : 03:49:54
That's absolutely brilliant. Many thanks


steve

-----------

Facts are meaningless. You could use facts to prove anything that's even remotely true!
Go to Top of Page
   

- Advertisement -