Greetings,
I’ve been scanning the internet on how to do this, but so far no luck. There are numerous solutions posted, especially on this website (thank you for that), but none seem to work for me. This will most probably be because I am only learning asp.net since 4 months now, and lack the necessary knowledge.
What I would like to do is set focus on the username input textbox when the page is loaded. I’ve seen many solutions, such as “defaultfocus”, body onload, etc, etc. Anyone can give me a simple way to accomplish this?
Our project is a MVC 2 Web application. We have a master site, which collects all the contentplaceholders. Here is the link to the logon.aspx
<div id="Clogin" onclick="logout();">
<asp:ContentPlaceHolder ID="LoginContent" runat="server" />
</div>
The LogOn.aspx is practically the same as the auto-generated one. And here is the textbox I would like to set focus on:
<div class="login-field">
<%: Html.TextBoxFor(m => m.UserName)%>
<%: Html.ValidationMessageFor(m => m.UserName) %>
</div>
How can I trigger the onload event, be it in the master page, or the LogOn page, so that the focus is generated on that particular textbox?
Thanks in advance, and apologies for having to rely on your knowledge.
Sincerely,
Nicolas
You will need to use javascript for this. If you are using jquery you could include the following script:
Another possibility is to do this in the body tag: