Call procedure when a user hits Enter in a TextBox


I have an TextBox. After the user has typed something and presses enter I want to call a
procedure called oInsertValues. How do I do this?

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technology NinjaCommented:
Hi Murray,

Ideally, you would do this
1. Bind an onchange(JavaScript) event with your textbox

The Textbox
<asp:TextBox ID="YOURTEXTBOX" runat="server" onchange="oInsertValues(this)"></asp:TextBox>

Open in new window

JavaScript - Please put it at the end of your page HTML
// Assuming your ASP.Net page has included JQuery
<script type = "text/javascript">
function oInsertValues() {
// Write a code here to check how many keypresses, since we sent the request
        type: "POST",
        url: "YOURASPXFILE.aspx/oInsertValues",
        data: '{values: "' + this.value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert("Error : " + response.d);
function OnSuccess(response) {
    // Handle your success Scenario

Open in new window

CodeBehind Web Method
<System.Web.Services.WebMethod()> _
Public Shared Function oInsertValues(ByVal values As String) As Bool
' Insert your code here
   Return True
End Function

Open in new window

It is generally set up so that after a particular number of key presses (you really do not want to send a request on every keystroke - it generally degrades the end user experience) - you will call the server side method

Let me know if you need further help.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.