Solved

Performing button click from textbox enter in VB.NET

Posted on 2013-11-24
2
685 Views
Last Modified: 2013-11-24
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
0
Comment
Question by:pbissegger
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
See if adding following line to your Page_Load works

Page.RegisterHiddenField("__EVENTTARGET", "btnSubmit")

http://www.hanselman.com/blog/ASPNETHowToCreateADefaultEnterButtonForFormsPostBacks.aspx


You can also add the password textbox to a panel and then set Panel.DefaultButton property

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton(v=vs.110).aspx
0
 

Author Closing Comment

by:pbissegger
Comment Utility
Fantastic ! Works like a charm !

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

Thanks, Peter
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now