In the initial days when i was new to development side and in some site i used to login straight away when i enter username and password and hit Enter key on my keyboard.
I used to think how is this happening but never had any answer but now i am going to show how thats possible.
In this post I will be talking about how to make any button Default button and set Focus on a particular text box in any page thats displayed.
Ex: Make a test site and go to default.aspx
in <body> tag write the following code
<form id=”frmExample” runat=”server” defaultbutton=”btnLogin” defaultfocus=”txtUserName”>
Welcome to the Example Web site from dilip
<tr><td><asp:TextBox ID=”txtUserName” runat=”server” Text=”UserName” /></td><td><asp:TextBox ID=”txtPassword” runat=”server” Text=”Password”/></td></tr>
<tr><td><asp:Button ID=”btnLogin” runat=”server” Text=”Login” onclick=”btnLogin_Click”/></td><td><asp:Button ID=”btnSignIn” runat=”server” Text=”Signup”/></td></tr>
Note: Here the beauty is that i have login and singnup button and i made defaultbutton as login button, and defaultfocus on username text box so whenever this page is viewed from any web browser focus will be on username text box and default button will be login button so when u hit enter it will invoke login button so simple.
Step 2: see carefully i have a function onclick=”btnLogin_Click” now press F7 key and go to .cs side of the same page or right click on the mouse and select view code .
and write the following code in .cs side
protected void btnLogin_Click(object sender, EventArgs e)
Response.Write(“Default Button was clicked and u are now logged In”);
Note: Here another concept when ever you enter key this method will be invoked and at the same time there wont be any other you can see that means response is closed just to make you feel that you really logged in but actually u are in same page.
the output is some this like this :
First Time when visited:
Then when enter key is pressed:
Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )