I have modified the login on the main page slightly. I deleted the PWS login and replaced it with one of the default Login controls in VWD. See
www.robgibson.ca . What I would like is to have the default focus set to the username text box within the control. I have tried a few of the solutions presented in the posts here but I can't seem to get anything to work.
This works for me. I'm sure there is a better way to do this by looking up the textbox id first rather than hard coding what we view in the page source. View the source in the browser to see the name of the control that asp.net uses, plug it in to the getElementById, and give
it a try.
' Initialize a stringbuilder object, much faster than string concatenation
' Do not do any string concatenations within the string builder or it defeats the purpose.
Dim scriptLoader
As New System.Text.StringBuilder()
With scriptLoader
.Append("<script type='text/javascript'>")
.Append(vbCrLf)
.Append("var txtBox=document.getElementById('ctl00_Main_LoginArea_Login1_UserName');")
.Append(vbCrLf)
.Append("if (txtBox!=null ) txtBox.focus();")
.Append(vbCrLf)
.Append("</script>")
' Register script with page
Me.ClientScript.RegisterStartupScript(Me.GetType(),
"onLoadCall", .ToString())
End With
I have modified the login on the main page slightly. I deleted the PWS login and replaced it with one of the default Login controls in VWD. See
www.robgibson.ca . What I would like is to have the default focus set to the username text box within the control. I have tried a few of the solutions presented in the posts here but I can't seem to get anything to work.
Suggestions?
Rob
This is why I so love ASP.NET 2.0. LoginForm is the id of the
asp:login control and UserName is the name of the textbox in that control. You should probably check to see if either the login or txt vars are nothing before you try to do the
SetFocus.
Protected Sub Page_Load(ByVal sender
As Object,
ByVal e As System.EventArgs)
Handles Me.Load
Dim login As System.Web.UI.WebControls.Login = LoginArea.FindControl("LoginForm")
Dim txt As TextBox = login.FindControl("UserName")
I'm using two text boxes for allowing user to enter two four digit numbers. After entering first 4 digit the focus must change to the second text box. Can u please help me with this issue
I have tried to set focus by all approaches you have suggested and all I know (most of are same). Problem here is focus is set to textbox but after that it lost. May be due to other process happen after main page load like load master page, load user controls.
I have used both master pages and user controls.
Please suggest any way so that I can retain the set focus in the text box.
None
0 Points
64 Posts
Set Focus on Username text box
Jan 19, 2006 09:42 AM|rgibson69|LINK
I have modified the login on the main page slightly. I deleted the PWS login and replaced it with one of the default Login controls in VWD. See www.robgibson.ca . What I would like is to have the default focus set to the username text box within the control. I have tried a few of the solutions presented in the posts here but I can't seem to get anything to work.
Suggestions?
Rob
Member
130 Points
542 Posts
Re: Set Focus on Username text box
Jan 19, 2006 01:12 PM|jwadsworth|LINK
Rob,
Are you using a LoginView control?
That's what I'm using. I tried this:
Dim tmpTextbox As New TextBoxtmpTextbox =
CType(LoginArea.FindControl("UserName"), TextBox) If Not tmpTextbox Is Nothing ThentmpTextbox.Focus()
End IftestBox.Focus()
The focus wont set to the textbox inside the loginview control.
However, the focus sets fine in the textbox outside the control.
I'm sure it can be done through javascript. I look at it a bit.
Extended Personal Site Starter kit
Member
130 Points
542 Posts
Re: Set Focus on Username text box
Jan 19, 2006 01:38 PM|jwadsworth|LINK
Rob,
This works for me. I'm sure there is a better way to do this by looking up the textbox id first rather than hard coding what we view in the page source. View the source in the browser to see the name of the control that asp.net uses, plug it in to the getElementById, and give it a try.
' Initialize a stringbuilder object, much faster than string concatenation ' Do not do any string concatenations within the string builder or it defeats the purpose. Dim scriptLoader As New System.Text.StringBuilder() With scriptLoader.Append("<script type='text/javascript'>")
.Append(vbCrLf)
.Append("var txtBox=document.getElementById('ctl00_Main_LoginArea_Login1_UserName');")
.Append(vbCrLf)
.Append("if (txtBox!=null ) txtBox.focus();")
.Append(vbCrLf)
.Append("</script>") ' Register script with page
Me.ClientScript.RegisterStartupScript(Me.GetType(), "onLoadCall", .ToString()) End With
Extended Personal Site Starter kit
Member
130 Points
542 Posts
Re: Set Focus on Username text box
Jan 19, 2006 01:57 PM|jwadsworth|LINK
I should of got this right the first time. Here is probably the best way.
Dim tmpTextbox As New TextBoxDim tmpLogin As New System.Web.UI.WebControls.Login
tmpLogin =
CType(LoginArea.FindControl("Login1"), System.Web.UI.WebControls.Login)If Not tmpLogin Is Nothing Then
tmpTextbox = CType(tmpLogin.FindControl("UserName"), TextBox)
End If
If Not tmpTextbox Is Nothing Then
tmpTextbox.Focus()
End If
Extended Personal Site Starter kit
Star
7692 Points
1845 Posts
Re: Set Focus on Username text box
Jan 19, 2006 08:17 PM|whighfield|LINK
This is why I so love ASP.NET 2.0. LoginForm is the id of the asp:login control and UserName is the name of the textbox in that control. You should probably check to see if either the login or txt vars are nothing before you try to do the SetFocus.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim login As System.Web.UI.WebControls.Login = LoginArea.FindControl("LoginForm")
Dim txt As TextBox = login.FindControl("UserName")
Page.SetFocus(txt)
End Sub
None
0 Points
64 Posts
Re: Set Focus on Username text box
Jan 20, 2006 12:57 PM|rgibson69|LINK
Thanks to both of you. That worked.
Rob
None
0 Points
64 Posts
Re: Set Focus on Username text box
Jan 20, 2006 01:03 PM|rgibson69|LINK
Jeremy,
Nice work on the skins!!
Rob
None
0 Points
14 Posts
Re: Set Focus on Username text box
Apr 05, 2007 07:14 AM|ObieOne|LINK
I came across this. Works great. Now even the default button is set if you press enter.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SetFocus(Login1)
Dim ctl As Control = Login1.FindControl("LoginButton")
Login1.Attributes.Add("onkeypress", String.Format("javascript:return WebForm_FireDefaultButton(event, '{0}')", ctl.ClientID))
End Sub
GoodLuck
Arno
None
0 Points
2 Posts
Re: Set Focus on Username text box
Feb 27, 2008 11:13 PM|nukewarm|LINK
This is what I use for a c# page.
<script runat="server">
protected void Page_Load(object sender, EventArgs e){
SetFocus(Login1.FindControl("UserName"));
}
</script>
Member
23 Points
32 Posts
Re: Set Focus on Username text box
Sep 02, 2008 09:01 AM|vpmragu|LINK
thanx nukewarm.
i got simple and easiest way man.
once again thank you ver much.
Software Engineer,
Bangalore.
None
0 Points
1 Post
Re: Set Focus on Username text box
Oct 07, 2008 02:54 AM|yupa|LINK
{
Login1.Focus();
}
The focus will be put on the first eligible control within the Login control - UserName textbox.
None
0 Points
4 Posts
Re: Set Focus on Username text box
Apr 17, 2009 10:54 AM|Wei Ke|LINK
I try this but it seems it doesn't set the focus in the UserName textbox.
Member
23 Points
32 Posts
Re: Set Focus on Username text box
Jun 24, 2009 11:43 PM|vpmragu|LINK
hi wei ke,
Use th following. it works.
protected void Page_Load(object sender, EventArgs e)
{
Login1.Username.Focus = True;
}
Software Engineer,
Bangalore.
Member
23 Points
32 Posts
Re: Set Focus on Username text box
Jun 24, 2009 11:46 PM|vpmragu|LINK
you can use this also.
This is what I use for a c# page.
<script runat="server">
protected void Page_Load(object sender, EventArgs e){
SetFocus(Login1.FindControl("UserName"));
}
</script>
Software Engineer,
Bangalore.
None
0 Points
4 Posts
Re: Set Focus on Username text box
Jun 25, 2009 09:13 AM|Wei Ke|LINK
THanks RaguNathan. I figured.
None
0 Points
1 Post
Re: Set Focus on Username text box
Jan 22, 2010 10:44 AM|RannyMeier|LINK
This works for me:
None
0 Points
2 Posts
Re: Set Focus on Username text box
Apr 16, 2010 05:15 PM|vigraman|LINK
hi,
I'm using two text boxes for allowing user to enter two four digit numbers. After entering first 4 digit the focus must change to the second text box. Can u please help me with this issue
Member
10 Points
36 Posts
Re: Set Focus on Username text box
Sep 10, 2010 10:48 AM|NWeb|LINK
Hi All,
I have tried to set focus by all approaches you have suggested and all I know (most of are same). Problem here is focus is set to textbox but after that it lost. May be due to other process happen after main page load like load master page, load user controls.
I have used both master pages and user controls.
Please suggest any way so that I can retain the set focus in the text box.
Member
11 Points
33 Posts
Re: Set Focus on Username text box
Oct 29, 2010 09:39 AM|creer|LINK
Or
Protected Sub Login1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
sender.Focus()
End Sub
None
0 Points
13 Posts
Re: Set Focus on Username text box
Mar 30, 2011 01:24 AM|anuradha.jayasena|LINK
can you try control.focus() method
ex:
Login1.Focus();
focus
Member
120 Points
120 Posts
Re: Set Focus on Username text box
Apr 14, 2011 01:44 PM|khanahmed|LINK
Suppose txtUserName is your Username textbox name
than try following code
txtUserName.Focus();