|
 |
aspx_beginners thread: Detecting enter key from within an asp:textbox control
Message #1 by "James S. Wile" <jsw@l...> on Thu, 2 Jan 2003 14:32:20
|
|
How do I detect when the user presses the Enter key while the cursor is
in an asp:textbox control so that I may trigger a button press event?
Under classic asp, I used to do this in client-side code in an
on_keypress event for the textbox, where I would test if
window.event.keycode = 13, but that no longer seems to work in asp.net
for an asp:textbox control. Any suggestions?
Message #2 by "Peter Lanoie" <planoie@n...> on Fri, 3 Jan 2003 11:54:53 -0500
|
|
James
You should still be able to do what you want with the client side code. You
can add client events to web controls (in your case "asp:textbox") by
assigning the string to that item's appropriate attribute in the aspx
code-behind file:
Protected WithEvents myTextBox As System.Web.UI.WebControls.TextBox
'...
myTextBox.Attributes.Add("onKeypress", "myKeypressJSFunction(this.form);")
' or
myTextBox.Attributes.Add("onChange", "myTextChangeJSFunction(this.form);")
You get the idea. This seems to be the easiest way I have found of setting
HTML attributes of web controls.
Here's the problem: tying that event to something meaningful in ASP on the
postback. Because it's very difficult to manually "talk back" from
client-side script to the .Net handling of the postback, the best suggestion
I can offer is some good-old HTML/JS/ASP. You can create a hidden form
field (just put it directly into the ASPX file) then set it's value from the
onKeyPress JS routine. Back in page_load (or wherever you deem appropriate)
you can capture that value by a standard Request("myClientEvent") check and
do what you need. Potentially, if you have a button on the form that's your
equivalent of hitting the enter key, you might be able to actually raise
that button's click event (or perhaps just call the event handler function),
such that you don't have to mess with things further. The regular .Net
button onClick event will be captured and executed.
One thing I highly recommend whenever doing client-side JS on .Net forms is
to pass the form object reference into functions that are called by events
in form elements. This way, you don't have to worry about referencing the
form by name in the document structure. .Net will name the form, so this
just bypasses an often problematic issue.
.aspx:
<input type="hidden" name="myClientEvent">
<script language="JavaScript">
function onKeypress(objForm){
//Check for [ENTER] key here
// Put whatever you want as the client event value:
// text, enumerator, etc.
objForm.myClientEvent.value = "EnterKey";
}
</script>
.aspx.vb in page_load():
myTextBox.Attributes.Add("onKeypress", "myKeypressJSFunction(this.form);")
'...
If Request("myClientEvent") <> "" Then
Select Case Request("myClientEvent")
Case "EnterKey":
'You may have to put in sender and
' eventargs parameters here...
'Look at the button's handler for more detail
myButton_onClick(...)
'More cases if needed...
End Select
End If
I hope I didn't leave anything out! :)
-Peter
-----Original Message-----
From: James S. Wile [mailto:jsw@l...]
Sent: Thursday, January 02, 2003 14:32
To: aspx_beginners
Subject: [aspx_beginners] Detecting enter key from within an asp:textbox
control
How do I detect when the user presses the Enter key while the cursor is
in an asp:textbox control so that I may trigger a button press event?
Under classic asp, I used to do this in client-side code in an
on_keypress event for the textbox, where I would test if
window.event.keycode = 13, but that no longer seems to work in asp.net
for an asp:textbox control. Any suggestions?
|
|
 |