Solved

Submitting data on pressing ENTER button from a form

Posted on 2003-10-29
8
1,908 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
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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

18 Experts available now in Live!

Get 1:1 Help Now