Gold
Gold

Reputation: 62464

how to catch key event on textbox?

if i have focus on textbox and i press enter or Esc

how to catch this event ?

Upvotes: 2

Views: 1769

Answers (1)

Daniel Peñalba
Daniel Peñalba

Reputation: 31857

You can use the JavaScript onkeyup event directly:

<input id="Text1" type="text" onkeyup="keyPress(event)" />

Ore use the C# alternative:

Text1.Attributes.Add("onkeyup", "keyPress(this)");

Then the script code:

<script type="text/javascript">
    function keyPress(e)
    {
        var textBox = document.getElementById('Text1');

        var keynum;

        if (window.event) // IE
            keynum = e.keyCode;
        if (e.which) // Other browser
            keynum = e.which;

        switch (keynum)
        {
            case 13:
                //enter key
                break;
            case 27:
                //esc
                break;
        }
    }
</script>

Here there are some guidelines to catch the keystrokes in JavaScript, with a sample at the bottom.

Upvotes: 5

Related Questions