javascript redirect in asp.net?

  • Thread starter Thread starter Dunc
  • Start date Start date
D

Dunc

I've got a Web page written in C#, and when it runs and the user presses
enter, it submits the first button on the form. As you'd expect. I want
the system to redirect to a different page when a user pressed enter from a
second textbox. I've used the line of code in the Page_Load:

SearchString.Attributes.Add("onKeyDown", "if (window.event) if
(window.event.keyCode == 13)
window.location='searchresults.asp?SearchString=' +
document.Form1.QuickSearch_SearchString.value;");

Problem is: it doesn't damn work. If I put an alert('test'); in the
beginning, it fires. However, it never actually gets past the conditions,
even though I've used (and tested!) this script on classic ASP pages; it
just submits the first button. I've tried changing it to onKeyPress, but
still no luck.

Has anyone successfully got this working, or can someone see something I'm
doing stupid here?

Thanks in advance,

Duncan
 
Did you try setting window.event.cancelBubble = true? This will prevent the
enter event on the textbox to be bubbled up the the higher event handler.

- Shuvro
 
I added that to my script:

SearchString.Attributes.Add("onKeyDown", "if (window.event) if
(window.event.keyCode == 13) {
window.location='venuesearchresults.asp?SearchString=' +
document.Form1.QuickSearch_SearchString.value; window.event.cancelBubble =
true; } ");

....but still no banana.

Any other thoughts? Or have I gone around that the wrong way?

Duncan
 
I think that the Form Submit on the ENTER key being pressed raises another
event other than the "OnKeyDown" which actually fires the form. You have to
write a handler for that event and set the cancelBubble=true;

Look up the list of events supported on a <INPUT type=Text> element. You
might want to try the onkeypress event.

- Shuvro


--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
 
Back
Top