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")
Page.SetFocus(txt)
End Sub
Please mark the most helpful post(s) as Answer Blog | I need more space:DropBox Referral
rgibson69
Member
317 Points
65 Posts
Set Focus on Username text box
Jan 19, 2006 01:42 PM|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
jwadsworth
Contributor
2378 Points
542 Posts
Re: Set Focus on Username text box
Jan 19, 2006 05:12 PM|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
jwadsworth
Contributor
2378 Points
542 Posts
Re: Set Focus on Username text box
Jan 19, 2006 05:38 PM|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
jwadsworth
Contributor
2378 Points
542 Posts
Re: Set Focus on Username text box
Jan 19, 2006 05:57 PM|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
whighfield
Star
11721 Points
1859 Posts
Re: Set Focus on Username text box
Jan 20, 2006 12:17 AM|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
Blog | I need more space:DropBox Referral
rgibson69
Member
317 Points
65 Posts
Re: Set Focus on Username text box
Jan 20, 2006 04:57 PM|LINK
Thanks to both of you. That worked.
Rob
rgibson69
Member
317 Points
65 Posts
Re: Set Focus on Username text box
Jan 20, 2006 05:03 PM|LINK
Jeremy,
Nice work on the skins!!
Rob
ObieOne
Member
10 Points
14 Posts
Re: Set Focus on Username text box
Apr 05, 2007 11:14 AM|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
nukewarm
Member
4 Points
2 Posts
Re: Set Focus on Username text box
Feb 28, 2008 03:13 AM|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>
vpmragu
Member
85 Points
35 Posts
Re: Set Focus on Username text box
Sep 02, 2008 01:01 PM|LINK
thanx nukewarm.
i got simple and easiest way man.
once again thank you ver much.
Software Engineer,
Bangalore.