0

我有一个登录控件,并且通过将其转换为模板来添加一个重置按钮。我还尝试使用 Find Control Method 清空文本。但我无法清空文本框。下面的 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 = "";

    }
4

2 回答 2

1

您是否尝试过 HTML 重置?

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

于 2012-05-09T12:00:56.553 回答
1

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

lgnLogin.UserName = "";

那应该做你想做的事。

于 2012-05-09T13:11:39.270 回答