Link to home
Start Free TrialLog in
Avatar of pbissegger
pbisseggerFlag for Canada

asked on

Performing button click from textbox enter in VB.NET

Hello,

I have a login box in my application - which has a username textbox, a password textbox and a Submit button.

I want the login subroutine to fire whether the user presses the Submit button, or if the user has the focus in the password textbox and presses enter.

Initially, I had coded it as follows (had 2 different handlers - one for the button and one for the textbox - both pointing back to the same subroutine)

    Sub LoginProc(sender As Object, e As System.EventArgs)
        
        ' Login by pushing the LOGIN button
        LoadPermissions(usernameTextbox.Text, passwordTextbox.Text)

    End Sub
    
    Sub LoginProc1(ByVal Source As Object, ByVal E As EventArgs)
   
        ' Login by pushing ENTER
        LoadPermissions(usernameTextbox.Text, passwordTextbox.Text)

    End Sub
    
    Sub LoadPermissions(ByVal Username As String, ByVal Password As String)
  
      .... Do my stuff here ...

    End Sub

Open in new window


with the associated HTML:

<asp:TextBox ID="passwordTextbox" runat="server" OnTextChanged="LoginProc1" autopostback="true" Width="44" TextMode="Password" />
<asp:Button runat="server" id="loginsubmitButton1" OnClick="LoginProc" class="formbutton" />

Open in new window


Realize now that that has been crappy code and everytime I login (still don't know why) both handlers are automatically invoked and the LoadPermissions subroutine is fired twice.

How can I clean up my code to just fire LoadPermissions once with one handler that works for both pushing the button, and using the ENTER button while the textbox is in focus ?

Thanks, Peter
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pbissegger

ASKER

Fantastic ! Works like a charm !

I already had a panel around my login, so I implemented the panel solution.

Thanks, Peter