BioXhazard
BioXhazard

Reputation: 518

VB.net Enter Key

I was given the following pseudo code in order to get the form that has focus and only allow the form I want to be submitted:

<script> var currentForm = document.forms[0];</script>
<form ...><input onfocus="currentForm = this.form;"/></form>
<form ...><input onfocus="currentForm = this.form;"/></form>

 function globalKeyPressed(event) {
   if (event.keyCode == ENTER) { // This is pseudo-code, check how to really do it
     currentForm.submit();
   }
 }

How would I do this for VB.net because VB.net doesn't accept System.Windows.Forms.KeyPressEventArgs. I also wanted to add that I can't have multiple forms on my website as it disrupts the loginview. So my 2 seperate 'forms' are really just a loginview and then an asp:textbox and asp:button by themselves without a form.

Upvotes: 0

Views: 1413

Answers (2)

Fermin
Fermin

Reputation: 36081

The above looks like a javascript function done on the client side, not a server side event. If that is the case then there will be no difference in VB.NET because it will also use Javascript as the client side language.

All you'll need to do is hook up a client side click event to your button and run the code as you've been given, changing it to (off of the top of my heaD)

if (event.keyCode == 13){
  //Submit form.
}

You could add the client side click by using attributes.add on the Page_Load event of your form, something like:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      textbox1.attributes.add("onKeyUp", "globalKeyPressed")
End Sub 

This would then cause the globalKeyPressed() event to be called from your textbox whenever the key is pressed. The globalKeyPressed() event will then cause the current form to be submitted.

Upvotes: 2

Joel Coehoorn
Joel Coehoorn

Reputation: 415705

With ASP.Net, the entire page always posts, generally right back to itself. Your VB.Net code has to handle events from the server controls you put on the page, like the click event of the button or the ViewChanged event of the LoginView.

Upvotes: 0

Related Questions