ASP 登录控件重置按钮

本文关键字:按钮 控件 登录 ASP | 更新日期: 2023-09-27 18:33:33

我有一个登录控件,我通过将其转换为模板来为其添加重置按钮。我还尝试使用查找控件方法清空文本。但是我无法清空文本框。下面的 HTML 和代码隐藏

法典:

 <asp:Login ID="lgnLogin" runat="server" MembershipProvider = "myCustomProvider" OnAuthenticate = "lgnLogin_Authenticate" DisplayRememberMe = "false" TitleText = "" UserNameLabelText = "Username" >
    <LayoutTemplate>
        <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
            <tr>
                <td>
                    <table cellpadding="0">
                        <tr>
                            <td align="right">
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                    ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                    ToolTip="User Name is required." ValidationGroup="lgnLogin">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                                    ControlToValidate="Password" ErrorMessage="Password is required." 
                                    ToolTip="Password is required." ValidationGroup="lgnLogin">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2" style="color:Red;">
                                <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                            </td>
                        </tr>
                        <tr>
                            <td align="right" colspan="2">
                                <asp:Button ID="resetButton" runat="server" Text="Reset" 
                                    onclick="resetButton_Click"/>
                            </td>
                            <td align="right" colspan="2">
                                <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" 
                                    ValidationGroup="lgnLogin" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </LayoutTemplate>
</asp:Login>

代码隐藏:

protected void resetButton_Click(object sender, EventArgs e)
    {
        TextBox username =  (TextBox)lgnLogin.FindControl("UserName");
        TextBox passsword = (TextBox)lgnLogin.FindControl("Password");
        username.Text = "";
        passsword.Text = "";
    }

ASP 登录控件重置按钮

你试过 HTML 重置吗?

http://www.echoecho.com/htmlforms13.htm

无论如何,密码都应该在回发时自动清除,但用户名存储在控件 ViewState 中,并在事件触发后重新填充文本框,因此您还需要像这样清除用户名字段 -

lgnLogin.UserName = "";

这应该做你想做的事。

供将来参考,也供将来可能涉及的任何人参考;您所要做的就是从代码隐藏中执行以下操作:

C#

protected void resetButton_Click(object sender, EventArgs e)
{
    ((TextBox)lgnLogin.FindControl("UserName")).Text = "";
    username.Text = "";
}

VB.NET

 CType(LoginAuth.FindControl("UserName"), TextBox).Text = ""
 LoginAuth.UserName = ""

密码文本框将自动清除。似乎我们需要将用户名.文本和登录名都设置为空字符串,就像我上面所做的那样。

祝你今天开心!