Pergunta

Using this ASP.Net Login Control, can we set the ID of the User Name TextBox? I would like to call it txtUserName.

<asp:LoginView 
    ID="loginViewMain" 
    runat="server">

    <LoggedInTemplate>
        <asp:LoginName 
            ID="loginName" 
            runat="server"
            FormatString="Hello, {0}!<br/><br/> You have successfully<br/> logged onto the staff site." />

        <br/>
        <br/>

        (<asp:LoginStatus ID="loginStatus" runat="server" />)

        <br/>
        <br/>

    </LoggedInTemplate>

    <AnonymousTemplate>
        <asp:LoginStatus 
            ID="loginStatus" 
            runat="server" />
    </AnonymousTemplate>
</asp:LoginView>

I would like to set focus on the User Name TextBox once I have a name for it from inside a code-behind file.

Foi útil?

Solução

In you aspx, if you add a login control (where the username, password and "remember me" button show") to your anonymous template

<asp:LoginView 
ID="loginViewMain" 
runat="server">

<LoggedInTemplate>
        <asp:LoginName 
            ID="loginName" 
            runat="server"
            FormatString="Hello, {0}!<br/><br/> You have successfully<br/> logged onto the staff site." />

        <br/>
        <br/>

        (<asp:LoginStatus ID="loginStatus" runat="server" />)

        <br/>
        <br/>

    </LoggedInTemplate>

    <AnonymousTemplate>
        <asp:LoginStatus 
            ID="loginStatus" 
            runat="server" />
        <asp:Login runat="server" ID="login"></asp:Login>
    </AnonymousTemplate>
</asp:LoginView>

In the page load event you can set focus

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        loginViewMain.FindControl("login").FindControl("UserName").Focus();

    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top