?
Solved

Submitting data on pressing ENTER button from a form

Posted on 2003-10-29
8
Medium Priority
?
1,923 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
Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

 

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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

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.   …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

801 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