ASP登录控制重置按钮

时间:2012-05-09 11:52:56

标签: c# asp.net .net login-control

我有一个登录控件,我已经通过将其转换为模板添加了一个重置​​按钮。我还尝试使用“查找控制方法”清空文本。但我无法清空文本框。下面是Html和Codebehind

代码:

 <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 = "";

    }

2 个答案:

答案 0 :(得分:1)

您是否尝试过重置HTML?

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

答案 1 :(得分:1)

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

lgnLogin.UserName = "";

那应该做你想要的。