• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 684
  • Last Modified:

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

0
wilcor14
Asked:
wilcor14
  • 4
  • 3
1 Solution
 
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
 
Todd GerbertIT ConsultantCommented:
Sorry...

add defaultbutton attribute to form tag.

<form id="form1" runat="server" defaultbutton="button1">
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now