0

I have 2 ASP textboxes for Login and Password. When I enter the password and click enter it should click the login button. Tried a lot from google like placing all of them in a panel. Can anyone help me with this?

Thanks

Password.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + LoginRequest.UniqueID + "').click();return false;}} else {return true}; ");

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34

2 Answers2

0

I'll give you the one that worked for me,

the fisrt part is added in page Load

txt_buscar.Attributes.Add("onkeypress", "cambiaFoco('btn_buscar')")

and the second part in the header area of you asp page or wherever you put your scripts

<script language="javascript" type="text/javascript">
    function cambiaFoco(cajadestino) 
    {
      var key = window.event.keyCode;              
      if (key == 13)  
        document.getElementById(cajadestino).focus();
    } </script>

just replace btn_buscar for your button ID, and txt_buscar for your password textbox ID

0

It is likely that the browser will handle this for you if you add an element to the form such as

<button type="submit">Login</button>

I would check the HTML before the JavaScript, I'm not intimately familiar with ASP but I remember seeing some strange form arrangements in similar web frameworks.

if you need to do it in JavaScript here is what I would recommend

Password.Attributes.Add("onkeydown", 
    "if(event.which && event.which == 13 || event.keyCode && event.keyCode == 13){
        document.getElementById(
           '" + LoginRequest.UniqueID + "'
        ).click();
     return true;
    "
);

it's unlikely that your input field will place a new line in the content, so there is no need to return false

Fire Crow
  • 7,499
  • 4
  • 36
  • 35