Link to home
Start Free TrialLog in
Avatar of tilakv
tilakv

asked on

Submitting data on pressing ENTER button from a form

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
SOLUTION
Avatar of NetPointer
NetPointer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tilakv
tilakv

ASKER

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
Avatar of tilakv

ASKER

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
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.
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tilakv

ASKER

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. :)
also see:
http://www.utmag.com/September2003/Page3.asp "Setting a Default Command Button in ASP.NET "