I would like to reach and clear the username textbox (which is in a loginbox) after the user typed his/her username and/or password incorrect. I tried this;

((TextBox)loginBox.FindControl("UserName")).Text = "";

but I was unable to clear the username textbox or to focus there?

有帮助吗?

解决方案

In login tool focus is possible by this code:

loginBox.FindControl("UserName").Focus();

But as far as I know it is not possible to clean UserName text. iI cannot be cleared like traditional methods. Similarly, most websites keep username but clean password field even user supplied wrong information.

You should change the "UserName" textbox attributes from the code to normal textbox attributes.

其他提示

It seems we need to set both the UserName.Text and Login.UserName to an empty string in some event like Login.OnLoginError:

<script runat="server">
    void OnLoginError(object sender, EventArgs e)
{
    ((TextBox)Login1.FindControl("UserName")).Text="";
    Response.Write("Hello!");
    Login1.UserName = "";
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Login ID="Login1" runat="server" OnLoginError="OnLoginError">

        </asp:Login>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
    </form>
</body>
</html>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top