7

My login usercontrol has two text boxes and a linkbutton.

<asp:TextBox id="tbUserName" runat="server" size="10" />
<asp:TextBox id="tbPassword" runat="server" TextMode="Password" size="10" />

<asp:LinkButton Text="login" CssClass="submit"  runat="server" ID="lbLogin" OnClick="btnLogin_OnClick" />

I would like to call the "btnLogin_OnClick function when someone pushes enter on tbUsername or tbPassword.

How do I do this?

abatishchev
  • 92,232
  • 78
  • 284
  • 421
Nate
  • 2,286
  • 4
  • 33
  • 52

2 Answers2

14

Here's a neat trick:

<asp:Panel ID="pnlLogon" runat="server" DefaultButton="lbLogin" Width="100%" >
        <asp:TextBox id="tbUserName" runat="server" size="10" />
        <asp:TextBox id="tbPassword" runat="server" TextMode="Password" size="10" />
        <asp:LinkButton Text="login" CssClass="submit"  runat="server" ID="lbLogin" OnClick="btnLogin_OnClick" />
</asp:Panel>

By wrapping the textboxes in a panel and setting the DefaultButton of the Panel to your LinkButton, any Enter in the text box inside the panel will cause the LinkButton Click to happen.

Mrchief
  • 70,643
  • 19
  • 134
  • 181
  • Thanks! This works in IE and Firefox but not chrome. Any ideas? – Nate Aug 27 '11 at 21:34
  • Maybe due to this? http://kpumuk.info/asp-net/using-panel-defaultbutton-property-with-linkbutton-control-in-asp-net/ – Mrchief Aug 27 '11 at 22:50
  • Brilliant, I had to follow the instructions in the link above for it to work in safari. Thank you. – Nate Aug 28 '11 at 00:50
0

// Code-behind

protected void btnLogin_OnClick(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        // process your form
    }
}
IrishChieftain
  • 15,072
  • 7
  • 47
  • 90