我使用asp.net登录控件来记录我的网站问题是我想添加一个选项来显示密码(而不是星号),但我无法访问密码文本框,所以我无法更改其文本模式。
任何的想法?
LayoutTemplate
在登录控件中可用,您可以完全更改布局以及您想要的任何其他内容。这是一个为我在一个项目中使用的登录页面设计的完整布局。
<asp:Login ID="Login1" runat="server">
<LayoutTemplate>
<table>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</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="Login1">*</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" ></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
</td>
<td>
<asp:CheckBox ID="RememberMe" runat="server" CssClass="hint hide" Text="Remember me next time." />
</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="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1" />
</td>
</tr>
</table>
</LayoutTemplate>
</asp:Login>
onchange="document.getElementById('password').type = this.checked ? 'text' : 'password'"
即使我们可以使用复选框的onclick事件。 Refference code
我制作显示/隐藏密码控件的方式包含了Muhammad Akhtar和Kesavarapu Venkatesh的答案。我使用像Muhammad这样的ASP.NET登录控件,但也使用了一个像Kesavarapu这样非常相似的代码的复选框。我的复选框和他之间的区别是JavaScript获得的ID,即“<%=((TextBox)logUsers.FindControl(”Password“))。ClientID%>”而不是“password”,因为我们正在访问ASP.NET登录控件而不是常规文本框。
<asp:Login ID="logUsers" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid" BorderWidth="1px" FailureText="" Font-Names="Verdana" Font-Size="10pt" OnAuthenticate="logUsers_Authenticate" UserNameRequiredErrorMessage="Username is required.">
<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
</asp:Login>
<input type="checkbox" onchange="document.getElementById('<%=((TextBox)logUsers.FindControl("Password")).ClientID %>').type = this.checked ? 'text' : 'password'" /> Show password