C# 如何在 asp:login 控件中禁用自动完成功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19538343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable autocomplete in asp:login control?
提问by akd
Is there a way of using autocomplete="off"
in <asp:Login> </asp:login>
. I tried to convert asp:login to template and put the autocomplete="off" attribute in asp:TextBox element however this breaks other part of login process. Is there a way of disabling autocomplete without using javascript and without converting to template. if its possible behind code is fine. Look forward to your suggestions. Thanks
有没有办法autocomplete="off"
在<asp:Login> </asp:login>
. 我试图将 asp:login 转换为模板并将 autocomplete="off" 属性放在 asp:TextBox 元素中,但这会破坏登录过程的其他部分。有没有一种方法可以在不使用 javascript 且不转换为模板的情况下禁用自动完成。如果它可能的背后代码没问题。期待您的建议。谢谢
Here is the code in Page_Load
这是 Page_Load 中的代码
if (!Page.IsPostBack)
{
var control = this.FindControlRecursive(LoginArea, "UserName") as TextBox;
if (control != null)
{
control.Attributes.Add("autocomplete", "off");
}
var control2 = this.FindControlRecursive(LoginArea, "Password") as TextBox;
if (control2 != null)
{
control2.Attributes.Add("autocomplete", "off");
}
}
And here aspx page:
这里是aspx页面:
<asp:Login ID="LoginArea" runat="server" SkinID="Login" CssSelectorClass="PrettyLogin"
DestinationPageUrl="Home.aspx" LoginButtonImageUrl=""
LoginButtonText="login button"
LoginButtonType="Button"
UserNameLabelText="username>"
PasswordLabelText="password"
TitleText="title"
RememberMeSet="false" DisplayRememberMe="false"
FailureText="failed"
ToolTip="tool tip"
PasswordRecoveryText=""
PasswordRecoveryUrl="urlforpasswordrecovery"
CreateUserText=""
CreateUserUrl="" OnLoggedIn="LogOn_LoggedIn"
OnLoggingIn="LogOn_LoggingIn" OnLoginError="LogOn_Error" >
</asp:Login>
回答by Monika
To turn off auto-complete for your entire form, all you need to do is add an attribute to your form tag, like this:
要关闭整个表单的自动完成功能,您需要做的就是向表单标签添加一个属性,如下所示:
<form id="Form1" method="post" runat="server" autocomplete="off">
Easy enough. Now you won't get the auto complete on any of the controls on the form, works for any browser that supports auto-complete. The HTML INPUT tags also support the use of autocomplete=off and since the control renders as INPUT tags then you can use it to set it on a control by control basis. Just add it to the TextBox at design-time (but note that VS.NET will underline it with a squiggly saying that textbox does not have an attribute for autocomplete - but it will still work):
很容易。现在您不会在表单上的任何控件上获得自动完成功能,适用于任何支持自动完成功能的浏览器。HTML INPUT 标记还支持使用 autocomplete=off 并且由于控件呈现为 INPUT 标记,因此您可以使用它在逐个控件的基础上设置它。只需在设计时将它添加到 TextBox(但请注意,VS.NET 会用波浪线下划线表示文本框没有自动完成的属性 - 但它仍然可以工作):
<asp:TextBox Runat="server" ID="Textbox1" autocomplete="off"></asp:TextBox>
or at runtime:
或在运行时:
Textbox1.Attributes.Add("autocomplete", "off");
回答by Uro? Goljat
Try this:
尝试这个:
.aspx
.aspx
<asp:Login runat="server" ID="login"></asp:Login>
Code behind
背后的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var control = FindControlRecursive(login, "Password") as TextBox;
control.Attributes.Add("autocomplete", "off");
}
}
public Control FindControlRecursive(Control root, string id)
{
Control first = null;
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
first = t;
break;
}
}
return root.ID == id ? root : first;
}
This is example for Password field. For user name use "UserName" instead of "Password" for the second parameter of FindControlRecursive
method.
这是密码字段的示例。对于用户名,FindControlRecursive
方法的第二个参数使用“UserName”而不是“Password” 。
回答by Alok Kumar
Follow the following steps
请按照以下步骤操作