Call procedure when a user hits Enter in a TextBox

Murray Brown
Murray Brown used Ask the Experts™

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?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chief Technology Ninja
Distinguished Expert 2018
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.

Murray Developer


Thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial