How to set enter key on a button in asp.net

{0 Comments}

Here I have created a small syntax to set a enter key on a button in asp.net. There is no such event in asp.net. We can do this with the help of JavaScript.

For doing this first take a asp.net button control then to assign a click event to it and all your associated code. After these the real magic starts, add following JavaScript to the head of your webpage

 Javascript |  copy code |? 
1
<script language="javascript" type="text/jscript">
2
function alertmsg(){
3
document.getElementById("Button1").click();
4
}
5
document.onkeypress=alertmsg
6
        </script>

Here the line “  document.onkeypress=alertmsg “ will detect the keypress of your keyborad then after that it will call javascript funtion named alertmsg here then after this funtion will find the your asp.net button by it ID which you have enter in the line “document.getElementById(“Button1″).click();”. all this is done now. 

Leave a Comment

Your email address will not be published.