Last post May 03, 2009 11:39 AM by shawn.bordeaux
May 03, 2009 10:36 AM|shawn.bordeaux|LINK
So i would think this would pretty simple. If I want to set focus on the login control of my login.aspx page how can I do that? Here is what I use to do and here is what I've tried with no success.
In my masterpage.master file
window.onload = document.getElementById('UserName').focus;
Then my body tag of the masterpage.master
And then my login control in login.aspx
<asp:TextBox ID="UserName" runat="server" Font-Size="0.8em"></asp:TextBox>
It does not focus. I would also like to focus on a few different fields througout my application. Any help solving this would be greatly appreciated!
May 03, 2009 10:53 AM|om_prakashp|LINK
The end quote is missing:
Please check the following url for more options:
May 03, 2009 11:16 AM|whighfield|LINK
If you look at the html source (Page -> View Source in IE) of your page find the UserName text box and look at what the ID is, ASP.NET renders the name of the controls a little funny (a hierarchy of controls). It might look like this ctl00_ContentPlaceHolder1_UserName.
On your UserName text box add "control_UserName" to the CssClass property (if you have a CssClass attribute already you can have multiple entries just make sure they have a space between them). So it may look like this
<asp:TextBox CssClass="control_UserName myRealStyleClass" ID="UserName" runat="server"></asp:TextBox>
Now what is going on here?
$(".control_UserName").focus(); using a Css selector (a jQuery thing like getElementByID) find the element with the Css class "control_UserName" (even though this css class does not exist) we are just using it kind of like a unique control name like UserName.
You can learn more about jQuery here
May 03, 2009 11:39 AM|shawn.bordeaux|LINK
That worked perfectly! Thank you!