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

Calling a subroutine if user presses Enter in a textbox

Status
Not open for further replies.

sbushway

Programmer
Jan 27, 2003
58
US
Hi,
I have a page where I'm expecting the user to input data in a textbox and then submit that data.

The problem occurs when the user presses Enter after he inputs his data; he expects the form to be submitted but nothing happens. He has to physically click the submit button in order for the page to be sent.

I saw on another forum where the suggestion was to add this to my Page_Load sub:

Code:
txtQuantity.Attributes.Add("onKeyUp", "if(window.event.keyCode==13){document.form1.submit();}")

The button to send the form isn't a submit button - it's just a regular button I added to the design view of my aspx page.

So what I was thinking was that instead of saying "document.form1.submit()", I'd call my btnSubmit_Click subroutine.

I gave it a whirl but nothing happened. My code looked like this:

Code:
Dim tempSend as System.Object
Dim tempEvent as System.EventArgs
txtQuantity.Attributes.Add("onKeyUp", "if(window.event.keyCode==13){btnSubmit_Click(tempSend, tempEvent);}")

Can I even call a sub like that?

Any help would be appreciated.

Thanks in advance!
Suzanne
 
I (finally) got it to work by using:

Code:
Private Sub TextBox1_EnterPressed(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
   If e.KeyCode = e.KeyData.Enter Then 
      'do something
   End If
End Sub

The Subroutine name is not important (the event handler is).

HtH,

Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top