Link to home
Start Free TrialLog in
Avatar of wilcor14
wilcor14Flag for United States of America

asked on

onkeyPress does not work in FF or Safari

The code below works in IE but not in FF or safari. Basically when a user has entered the username and password and enter is hit, the submit button should be triggered. Is there a way we can do this in FF? Thanks.
<div onkeypress="if (event.keyCode == 13) document.getElementById('ctl00_LoginView1_Login1_btnLogin').click();">
                                                                            <asp:TextBox ID="UserName" runat="server" Font-Names="Verdana" Font-Bold="True" Font-Size="XX-Small"
                                                                                BorderWidth="1px"></asp:TextBox>
                                                                        </div>

Open in new window

Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Probably the text box is receiving the onKeyPress event, preventing the DIV from seeing it.  You should be able to add the onKeyPress to the <asp:TextBox> tag - I believe the .Net rendering engine ignores tag attributesit doesn't recognize, like onKeyPress, and sends them unprocessed to the client.

You can also do it in code-behind, which might be better, using

UserName.Attributes["onKeyPress"] = btnLogin.ClientID + ".click();"
Avatar of wilcor14

ASKER

Thanks for the input.

I put the onKeyPress within the TextBox tag but it did not work. The code behind didn't work either. Any other suggestions?
Sorry...

add defaultbutton attribute to form tag.

<form id="form1" runat="server" defaultbutton="button1">
That didn't work but what does work is if I put the following:

I also put the code I have for  my login button. Thanks

<asp:TextBox ID="txtUserName" runat="server" Font-Names="Verdana" Font-Bold="True" Font-Size="XX-Small"
                                                onkeypress="if (event.keyCode == 13) alert('enter');" BorderWidth="1px"></asp:TextBox>

<asp:LinkButton ID="btnLogin" OnClick="btnLogin_Click" Text="Member Login" runat="server"
CommandName="Login" ValidationGroup="Login1"> Login                </asp:LinkButton>

Open in new window

Hmm...this works for me.
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        label1.Visible = true;
    }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" defaultbutton="btnLogin">
    <div>
        <asp:Label ID="label1" runat="server" Text="Button 1 Clicked" ForeColor="Red" Visible="false"></asp:Label>
        <hr />
        <asp:TextBox ID="textBox1" runat="server"></asp:TextBox>
        <hr />
        <asp:LinkButton ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click"></asp:LinkButton>
    </div>
    </form>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Aha, that gave me an idea,
I added the following:

<asp:Button ID="btnHiddenLogin" OnClick="btnLogin_Click" Text="Member Login" runat="server"
CommandName="Login" ValidationGroup="Login1" style="display:none;" />
Now I can use .click(); and it will work.
The reason I don't want to use the defaultbutton option is because I have my login options in my masterpage and that complicates when users hit return when they are on other pages. But this work around works good. thanks.