[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Performing button click from textbox enter in VB.NET

Posted on 2013-11-24
2
Medium Priority
?
761 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 39672789
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
ID: 39673366
Fantastic ! Works like a charm !

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

Thanks, Peter
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

650 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