Solved

Submitting data on pressing ENTER button from a form

Posted on 2003-10-29
8
1,922 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 60 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 

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 65 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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 …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

705 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