Can we set focus in UserName of a LoginView

A

ad

I use a LoginView to authenticate user.
Can we set the focus in the UserName or Password of the LoginView?
 
N

needin4mation

here is one I found and modified it for csharp:

private void SetFocusControl(String ControlName)
{
//http://www.developer.com/net/asp/article.php/2237431
//character 34 = "

String script = "<script language=\"javascript\""+
">" +
" var control = document.getElementById(\"" +
ControlName + "\");" +
" if( control != null ){control.focus();}" +
"</script>";

Page.RegisterStartupScript("Focus", script);
}
 
A

ad

Thanks,
If the UserName control is general textbox, It can do by SetFocus or
Javascript.
But now the UserName is in LoginView.
How can I do?
 
Joined
Jun 27, 2006
Messages
1
Reaction score
0
Username focus in a LoginView

I also had this page inside a master.page but this is what worked for me in the PreRender event ...



Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

'Find the Login control inside the LoginView
Dim curLogin As System.Web.UI.WebControls.Login = CType(LoginView1.FindControl("Login1"), System.Web.UI.WebControls.Login)

'Then find the Username textbox control
Dim txtUsername As TextBox = CType(curLogin.FindControl("UserName"), TextBox)

'Now set the focus on it
txtUsername.Focus()
End Sub
 
Joined
Mar 21, 2007
Messages
1
Reaction score
0
C# solution to LoginView SetFocus

Thanks for the ideas in VB.

Here is a C# solution that workes for me.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if(!User.Identity.IsAuthenticated)
{
Login lg = (Login)LoginView1.FindControl("Login1");
TextBox tb = (TextBox)lg.FindControl("UserName");
SetFocus(tb);
}
}
}

BTW notice that tb does not have double quotes around it in the SetFocus (tb). I was pulling my hair out following another thread that had quotes around the control string inside the SetFocus method. If I can find that post again, I'll put in a correction there too!

John
 
Joined
Dec 26, 2007
Messages
1
Reaction score
0
It works!

jnewquis said:
I also had this page inside a master.page but this is what worked for me in the PreRender event ...



Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

'Find the Login control inside the LoginView
Dim curLogin As System.Web.UI.WebControls.Login = CType(LoginView1.FindControl("Login1"), System.Web.UI.WebControls.Login)

'Then find the Username textbox control
Dim txtUsername As TextBox = CType(curLogin.FindControl("UserName"), TextBox)

'Now set the focus on it
txtUsername.Focus()
End Sub


Hi! This really works. Thanks
 
Joined
Mar 17, 2009
Messages
1
Reaction score
0
it is working in IE but not working in Firefox.
I do something like this:
TextBox usernametxt = (TextBox)this.logOn.FindControl("UserName");
SetFocus(usernametxt);

on the aspx page I have code like this....
<asp:Login ID="logOn" runat="server".......>
<LayoutTemplate>
......
<asp:TextBox ID="UserName" runat="server" Width="250" TabIndex="1"></asp:TextBox>
......
</LayoutTemplate>
</asp:Login>

Can someone help me with this?
jostar said:
Thanks for the ideas in VB.

Here is a C# solution that workes for me.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if(!User.Identity.IsAuthenticated)
{
Login lg = (Login)LoginView1.FindControl("Login1");
TextBox tb = (TextBox)lg.FindControl("UserName");
SetFocus(tb);
}
}
}

BTW notice that tb does not have double quotes around it in the SetFocus (tb). I was pulling my hair out following another thread that had quotes around the control string inside the SetFocus method. If I can find that post again, I'll put in a correction there too!

John
 
Joined
Jun 10, 2010
Messages
1
Reaction score
0
LoginView.Focus Method

thank you very much jnewquis, very much appreciated.
linked to msdn community content - LoginView.Focus Method

thanks again jnewquis :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top