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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open new url from textbox value - doesn't work 1st time but does 2nd 1

Status
Not open for further replies.

may1hem

Programmer
Jul 28, 2002
262
GB
I want people visiting my web site to have directions to my office using Google Maps, which uses a simple querystring for directions. So on my web site, I want a textbox where a customer can enter where they're coming from, then they either press enter or click a submit button, and then Google Maps should load in a new window.

So I tried this code in the HTML body:
<form runat="server" id="frmMap">
Coming from: <asp:TextBox runat="server" ID="txtComingFrom"></asp:TextBox>
<asp:Button runat="server" ID="btnSubmit" Text="Submit" />
</form>

And I inserted this in the Page_Load:
btnSubmit.OnClientClick = "window.open(' & txtComingFrom.text & "+to+SW1P+2NU');"

When I type a starting location in the text box and press enter (or click submit) then the new window opens ok, but the value of txtComingFrom.text is empty. The strange thing is that if I click submit again (or click in the text box and press enter) then the value of txtComingFrom.text correctly appears. Every time I try this it's the same, clicking Submit once and the textbox value isn't picked up, but if I click it a second time then it is. Why is this?

I have also tried different code:
btnSubmit.Attributes.Add("onclick", IIf(Trim(txtComingFrom.Text) = "", "window.open(' was empty.');", "window.open(' & Trim(txtComingFrom.Text) & "+to+SW1P+2NU');alert('Textbox contains " & Trim(txtComingFrom.Text) & ".');"))

But this alternative code produces the same results, the first click brings up a message "Textbox was empty", and clicking the submit button a second time without reloading or changing anything correctly brings up an alert message with the textbox contents. Why is this?

It seems ASP.net doesn't pick up the textbox value the first time that the submit button is clicked. But it does pick it up when the button is click again. Surely this isn't the way ASP.net was designed? I must be doing something wrong? Can someone please help me here?
 
Of course the txtComingFrom.text will initially be blank. Think about it. The page loads the first time, the value in the textbox is blank and the window.open string will concatenate a blank for the textbox value. The button is then clicked and the client code runs BEFORE the server code and post-back. The second time around the value is there because you are using an asp server button which causes a post back. The value is posted along with any other properties set and therefore will show up in the textbox(whether viewstate is enabled or not on the textbox or page) and then your string is created correctly.
In your case, you need to have the onClientClick call a javascript function that creates the string there and then opens the window with the correct URL.
 
Thanks it works!

Also took me an hour to work out that JavaScript is case sensitive.. grrrrr..

FYI:

------------------
<form runat="server" id="frmTryClientClick">
Start from: <asp:TextBox runat="server" ID="txtTryClientClick" Width="130"></asp:TextBox>
<asp:Button runat="server" ID="btnTryClientClick" Text="Directions" OnClientClick='openurl()' />
</form>

<script language="javascript" type="text/javascript">
function openurl()
{
var url=' + document.getElementById("<% =txtTryClientClick.ClientID %>").value;
window.open(url);
return
}
</script>
------------------
 
Yeah, I'm not a fan of js either, but it is very necessary in web applications.

Glad you got it working.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top