views:

236

answers:

2

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.

+2  A: 

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.

Fermin
But what would the code look like?
BioXhazard
I don't understand. Where should the second part go? And I keep getting an error for those form tags.
BioXhazard
@Andrew - ASP.Net webforms only allows you to have one main form. You can't put in your own smaller forms on the page.
Joel Coehoorn
Yeah, I never said I did. That's why I used a loginview and then just a free textbox and button.
BioXhazard
Another problem with what you did, the code can't seem to find the textbox in my loginview because it's so far in.
BioXhazard
What do you mean 'so far in'?
Fermin
Inside the login view is an asp:login. Inside that is a layout template. Inside of that is the textbox.So the question is, how would I get a textbox from such a specific location within 3 tags.
BioXhazard
A: 

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.

Joel Coehoorn