简体   繁体   English

将选项“显示密码”添加到 asp.net 登录控制

[英]add the option “show password ” to asp.net login control

I used asp.net login control in ored to log my site the issue is that I want to add an opption to show the password (instead of asterisk) but I have no access to the password textbox so I can't change its textmode.我使用 asp.net 登录控件来登录我的网站,问题是我想添加一个选项来显示密码(而不是星号),但我无法访问密码文本框,因此我无法更改其文本模式。

Any Idea?任何想法?

LayoutTemplate is available in the login control, you can completely change the layout and whatever else you want. LayoutTemplate在登录控件中可用,您可以完全更改布局以及您想要的任何其他内容。 Here is a completeled layout designed for a login page that I have used in one project.这是为我在一个项目中使用的登录页面设计的完整布局。

 <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">
                    &nbsp;
                </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'"

Even we can use the onclick event of the checkbox.甚至我们可以使用复选框的 onclick 事件。 Refference code 参考代码

The way I made my show/hide password control incorporated both Muhammad Akhtar's and Kesavarapu Venkatesh's answers.我制作显示/隐藏密码控制的方式结合了 Muhammad Akhtar 和 Kesavarapu Venkatesh 的答案。 I used the ASP.NET Login control like Muhammad, but also used a checkbox with very similar code like Kesavarapu.我像 Muhammad 一样使用了 ASP.NET 登录控件,但也使用了代码非常相似的复选框,例如 Kesavarapu。 The difference between my checkbox and his was the ID the JavaScript is getting, which is "<%=((TextBox)logUsers.FindControl("Password")).ClientID %>" instead of "password", since we are accessing the ASP.NET login control instead of a regular textbox.我的复选框和他的复选框之间的区别在于 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM