我有一个登录控件,并且通过将其转换为模板来添加一个重置按钮。我还尝试使用 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 = "";
}