views:

102

answers:

1

I have two form elements on my page that act as a smooth login panel. When you activate the panel, you can enter in your email address and your password to login. Next to them is an <asp:LinkButton... with a C# function bound as a click handler. That function will post to another login page (we must do it this way based on how our app is interacting with the other site) and set some login cookies. When I enter in my credentails and physically click the button, everything works fine. When I enter my email, then my password, then hit the [ENTER] key, the click handler of the login button does not fire. Ok, so what? I've dealt with this before. Here are all of the methods I've tried to fix this with no success:


Added an extra hidden textbox after the login button

<asp:TextBox ID="tb1" runat="server" Style="display:none; visibility:hidden;"></asp:TextBox>

No success.


Set the DefaultButton attribute on the form (since I'm in a MasterPage):

<form DefaultButton="lbLogin">

No success.


Wrapped my fields in an <asp:Panel ... > and set the DefaultButton attribute

<asp:Panel ID="pnlFields" DefaultButton="lbLogin" runat="server">... my stuff here ...</asp:Panel>

No success.


Started to capture the enterpress via jQuery:

// if I hit enter on ANY input, simulate a click of the login button
$(".login-fields input").keypress(function(e) {
  if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) {
    $(".btnLogin").click();
    return false;
  }
});

$(".btnLogin").click(); didn't work so I tried $(".btnLogin").trigger("click");. No good. I also tried $(".btnLogin").submit(); which I knew wouldn't work (but I tried anyway).

Since there's a return false this just does nothing, so I removed it and tried the scenarios again. No dice.

I've read before that keypress might not be a good JavaScript event so I tried keyup as well with the above scenarios and still got nothing.


Now, here's the ONLY thing that seems to work and it involves using JavaScript eval():

// if I hit enter on ANY input, simulate a click of the login button
$(".login-fields input").keypress(function(e) {
  if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) {
    eval( $(".btnLogin").attr("href") ); // BAD! right?
  }
});

Does anyone know how I can get this to work without doing the eval() in JS? For reference, this is the actual generated href of the login link:

javascript:__doPostBack('ctl00$lbLogin','')
+1  A: 
<asp:Panel runat="server" ID="pnlWrapper" DefaultButton="btnHidden">
   <asp:LinkButton runat="server" ID="btnVisible" Text="Login" OnClick="btnVisible_Click" />
   <asp:Button runat="server" ID="btnHidden" style="display:none;" OnClick="btnVisible_Click" />
</asp:Panel>

Voila.

ianpoley
So it looks like what you've done is made both my real button and a hidden `<asp:Button>` both fire the same handler and you set the `DefaultButton` to fire on the **hidden** button. It worked! +10000
Mark Ursino