Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a login page, i have set default button property of form with login button. so when I press enter key it must login.

when i am not using focus on ony one of the textboxes(username,password) it working fine.
but when i have used focus method on any one of the textbox for the first time when i press enter key its not accepting, for second time its accepting

above case is when i have saved my password in chrome and when i open login page it automatically loads textbox values i.e., username,password.

following is the code:

ASPX:

XML
<form id="form1" runat="server" defaultbutton="btnSubmit">
        <div>
            <table>
                    <tr>
                        <td>UserName:
                        </td>
                        <td>
                            <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rfvUserName" runat="server" ErrorMessage="Enter UserName" ControlToValidate="txtUserName"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>Password:
                        </td>
                        <td>
                            <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rfvPassword" runat="server" ErrorMessage="Enter Password" ControlToValidate="txtPassword"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                        </td>
                    </tr>
                </table>
        </div>
    </form>



CS Code:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            form1.Attributes.Add("onkeydown", "javascript: return WebForm_FireDefaultButton (event, '" + btnSubmit.ClientID + "')");
            txtUserName.Focus();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Response.Redirect("home.aspx");
    }

please do help me here.




thanks in advance.
Posted
Updated 30-Oct-14 20:37pm
v3
Comments
King Fisher 31-Oct-14 2:33am    
What' your Code? Update it.
ClimerChinna 31-Oct-14 2:38am    
I have posted my code, check the question again.
King Fisher 31-Oct-14 2:52am    
Why don't you use With Panel, Simple :
<asp:Panel ID="panel" runat="server" DefaultButton="button">
<asp:Button ID="button" runat="server" Text="this is the button" />

try below code ...

JavaScript
if (event.keyCode == 13) {
      ....your code 
}
else
{
document.getElementById("txtUserName").focus(); 
}
   }
 
Share this answer
 
by removing below line i have solved it myself

form1.Attributes.Add("onkeydown", "javascript: return WebForm_FireDefaultButton (event, '" + btnSubmit.ClientID + "')");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900