views:

146

answers:

3

I have a search form and a login form on my website. When the enter button is pressed when the login form has focus, the search runs instead of the login. Is there a way to fix this?

I've already tried using a panel around the login form and use defaultbutton, but the loginview errors when I do this.

A: 

You could try setting up a keypress event on your login form. Off the top of my head, something like

$('#loginForm').keypress(function (e) {
  if(e.keyCode=='13') //Keycode for "Return"
     $('#login').click();
  }
});

should work, assuming you give appropriate IDs to the elements involved.

Inaimathi
A: 

could you try adding attributes at run time like this

Login loginControl = (Login)lvLoginView.FindControl("logLogin"); 
TextBox tbUserName = (TextBox)loginControl.FindControl("UserName");
TextBox tbPassword = (TextBox)loginControl.FindControl("Password");
Button loginButton = (Button)loginControl.FindControl("LoginButton");  
tbUserName.Attributes["onKeyPress"] = "KeyDownHandler('" + loginButton.ClientID + "')";
tbPassword.Attributes["onKeyPress"] = "KeyDownHandler('" + loginButton.ClientID + "')";

and some JS:

function KeyDownHandler(btn){  
// process only the Enter key  
if (event.keyCode == 13)  {    
// cancel the default submit  
  event.returnValue = false;  
  event.cancel = true;   
  var obj = document.getElementById(btn);   
  obj.click();  
}}

UPDATE

Auto-converted to VB.NET courtesy of telerik

Dim loginControl As Login = DirectCast(lvLoginView.FindControl("logLogin"), Login)
Dim tbUserName As TextBox = DirectCast(loginControl.FindControl("UserName"), TextBox)
Dim tbPassword As TextBox = DirectCast(loginControl.FindControl("Password"), TextBox)
Dim loginButton As Button = DirectCast(loginControl.FindControl("LoginButton"), Button)
tbUserName.Attributes("onKeyPress") = "KeyDownHandler('" + loginButton.ClientID + "')"
tbPassword.Attributes("onKeyPress") = "KeyDownHandler('" + loginButton.ClientID + "')"
Lerxst
By runtime do you mean form load? Because that's all done in VB.
Andrew
Okay, well the code that I put should be quite easy to convert into VB, its just C#
Lerxst
A: 

If you're talking about HTML, then this suggests tab order (learn more about that here) is relevant or the order in which the forms were created, the behavior depending on the user agent.

Jan Kuboschek