Capture keystrokes in an ASP.NET text field

Is there an easy way to capture a keystroke (in my case: F5) in an ASP.NET text box and execute the server method as an answer?

I created a simple SQL interface and out of habit (from SQL Server Mgmt Studio), I continue to press F5 when I type in my query, but it always updates the browser, and does not execute my SQL query :-)

Can I do this in ASP.NET using maybe Javascript or jQuery?

Mark

UPDATE:

I have this jQuery code in my ASP.NET page:

 $(document).ready(function() {
    $("#<%= txtQuery.ClientID %>").keydown( function (e) {
        if (e.keycode == 116) {
           $("#<%= btnExecute.ClientID %>").trigger('click');
        }
    });
 });

"e.which", "e.keycode" , . MS IE 7 dev. txtQuery - ASP.NET, , btnExecute - ASP.NET, SQL Server .

? - ? : ?

+3
4

. keypress (fn)

$("#yourtextboxId").keypress( function (e) {
   // do stuff here
});

e.which

, , F5 116

trigger

$("#yourexecutescriptbutton").trigger('click');

Edit:

F5, keydown

$("#yourtextboxId").keydown( function (e) {
        if ( e.which === 116 )
        {
            $("#yourexecutescriptbutton").trigger('click');  
        }
    });
+2

, javascript , AJAX .

, . jQuery keypress :

$("#mytextbox").keypress(function(e){
    // Use e.which, etc to find out which key was pressed.
});

, (F1-F12) , keypress. , keydown keyup . , , , .

- ASP.NET. - (, , ), WebMethodAttribute:

[WebMethod]
public static string MyMethod(int foo, int bar, string bob)
{
    return "Welcome to The World of Tomorrow!";
}

, jQuery - :

var request = $.ajax({
    type: "POST",
    url: "MyPage.aspx/MyWebMethod",
    data: "{ foo:22, bar:19, bob:'A string' }",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        var response = null;

        if (typeof (msg.d) != 'undefined')
            response = msg.d;

        if (response == null)
            Error('Message returned by ASP.NET AJAX call is not in the expected format.');

        alert(response);
        },
    error: function(request, textStatus, errorThrown) {

        if (console && typeof console.log != "undefined") {

            try {
                var result = eval("(" + request.responseText + ")");

                if (result && typeof result.Message != "undefined")
                    console.log(result.ExceptionType + ": " + result.Message + "\n" + result.StackTrace);
            }
            catch (ex) {
                console.log(request.responseText);
            }
        }
    }
});

" !", ASP.NET.

, , , , , . . !

+3

, . false :

$("#yourtextboxId").keydown( function (e) {
    if ( e.which === 116 )
    {
        $("#yourexecutescriptbutton").trigger('click');
        return false; // Stops keydown event being bubbled.  
    }
});

, , , keydown keyup .

+1

You just get the ASP.NET text field client id in jquery. Then call ajax through jquery as shown below.

var aspTextBox = $("#<%=Me.TextBox1.ClientId%>");
aspTextBox.keypress(function(e){
//Code for $.ajax request
    if(e.which==13){
    $.ajax({
    type:"POST",
    url:"WebService.asmx/GetData",
    contentType:"application/json;charset=utf-8",
    dataType:"json",
    data:"{}",
    success:function(data){
        },
    error:function(a,b,c){
        },
    });
    }
});
0
source

Source: https://habr.com/ru/post/1716292/


All Articles