[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Submitting data on pressing ENTER button from a form

Posted on 2003-10-29
8
Medium Priority
?
1,925 Views
Last Modified: 2008-03-04
I have a form where I am submitting a request to the server. I have a text box and a Submit button in the form. I am coding it in VB.Net
I want the data to be submitted if I press Enter button from the text box OR if I click the Submit button.
The Enter button from the text box is not working. So I wrote the functionality in a method (lets assume SubmitData() and am calling it in both events (Button1_Click & TextBox1_TextChanged )

If I just hit Enter button from the text box it is working. The problem is that if I click on the Submit button after entering data into the textbox, then the method is being called twice (from both events)
How do I avoid that from happening.
I initially thought that the Button1_Click shoould have been fired automatically when I hit the Enter button. But it didnt work, so I wrote it in a method and called from TextBox1_TextChanged event. But now I have this problem.

Anybody knows how do I fix this ??

Thanks in advance
0
Comment
Question by:tilakv
[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
8 Comments
 
LVL 4

Assisted Solution

by:NetPointer
NetPointer earned 240 total points
ID: 9645875
instead of doing all this things, set the Forms accept button property to your button...

if its asp.net, then just declare the type of that button as submit buttton..

i think this should solve yr problem..

if not tell me..

Regards,
NetPointer
0
 

Author Comment

by:tilakv
ID: 9645908
How do I set the Forms accept button property to the button ?
Also how do I declare the type of that button as submit buttton ?

can you explain in detail ? thanks
0
 

Author Comment

by:tilakv
ID: 9645916
This is what I have:

<asp:button id="Button1" tabIndex="5" runat="server" Text="Add Notification"></asp:button>

and in the VB code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SubmitData()
    End Sub

    Private Sub email_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles email.TextChanged
        SubmitData()
    End Sub
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Expert Comment

by:Tiare
ID: 9646788
On form load put the line.

me.AcceptButton = cmdSubmit

or whatever your submit buttons name is.

:-)
but give the points to the other guy, cos it was his idea.
0
 
LVL 2

Expert Comment

by:adomsg
ID: 9647018
sorry, but if Tiare will help, what about if it is a web application.. hw to submit data on pressing ENTER button from a web.

thanks.
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 260 total points
ID: 9650060
You can make the button default with javascript

'we can have multiple textboxes and buttons. This code will make the button default.
'On text change event enter this code and designate which button make default



Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        DefaultButton(Page, TextBox1, Button1)
    End Sub


    Public Sub DefaultButton(ByRef Page As System.Web.UI.Page, ByRef objTextControl As TextBox, ByRef objDefaultButton As Button)
        ' Sets default buttons.
        Dim sScript As New System.Text.StringBuilder

        sScript.Append("<SCRIPT language=""javascript"">" & vbCrLf)
        sScript.Append("function fnTrapKD(btn){" & vbCrLf)
        sScript.Append(" if (document.all){" & vbCrLf)
        sScript.Append("   if (event.keyCode == 13)" & vbCrLf)
        sScript.Append("   { " & vbCrLf)
        sScript.Append("     event.returnValue=false;" & vbCrLf)
        sScript.Append("     event.cancel = true;" & vbCrLf)
        sScript.Append("     btn.click();" & vbCrLf)
        sScript.Append("   } " & vbCrLf)
        sScript.Append(" } " & vbCrLf)
        sScript.Append("}" & vbCrLf)
        sScript.Append("</SCRIPT>" & vbCrLf)

        objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." & objDefaultButton.ClientID & ")")
        Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString)
    End Sub
0
 

Author Comment

by:tilakv
ID: 9650862
me.AcceptButton = cmdSubmit

tried this but it gives an error that AcceptButton is not a member.
anyway I fixed this by another crooked way.
declared a boolean variable with defaul value as false. then inside the SubmitData function, I set the value to true when it is called the first time. I wrote a If condition around the code to check if the value is false. this way I avoid the method body being executed twice. I will split points between NetPointer and iboutchkine for the idea and effort

thanks to Tiare also. :)
0
 
LVL 3

Expert Comment

by:the-edge
ID: 9654082
also see:
http://www.utmag.com/September2003/Page3.asp "Setting a Default Command Button in ASP.NET "
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

656 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