How to check if enterkey is pressed in a TextBox in asp.net

前端 未结 4 1673
一整个雨季
一整个雨季 2020-12-22 04:47

I have a asp.net Text Box which accepts date in the format MM/DD/YYYY. After entering the date i will hit enter key.if enter key i need to execute server side code.How can w

4条回答
  •  醉酒成梦
    2020-12-22 04:49

    Use the keydown event:

    textBox.Attributes.Add("onKeyDown", "KeyDownHandler()");
    

    Have KeyDownHandler() check the pressed key and if correct, submit the surrounding form or invoke an AJAX request. See keyboard codes at Javascript Char Codes.


    Alternatively, textbox ID can be injected into jQuery from .aspx and assigned a direct keydown handler:

    $('#<%=textBox.clientID%>').keydown(function (e) { /* Do server request; */ });
    

提交回复
热议问题