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

wilcor14Asked:
Who is Participating?
 
Todd GerbertIT ConsultantCommented:
When you set the defaultbutton attribute of the <form> tag, ASP.Net automatically inserts the necessary onKeyPress event for the form tag.

Also, you can't call .click() on an <a> tag, which is what <asp:LinkButton>'s end up as on the client.

0
 
Todd GerbertIT ConsultantCommented:
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();"
0
 
wilcor14Author Commented:
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?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Todd GerbertIT ConsultantCommented:
Sorry...

add defaultbutton attribute to form tag.

<form id="form1" runat="server" defaultbutton="button1">
0
 
wilcor14Author Commented:
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

0
 
Todd GerbertIT ConsultantCommented:
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

0
 
wilcor14Author Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.