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
Here is the code in 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:
<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>
Use the <input> tag with autocomplete attribute. Set the autocomplete attribute to value “off”.
Follow the following steps
Try this:
.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.
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):
<asp:TextBox Runat="server" ID="Textbox1" autocomplete="off"></asp:TextBox>
or at runtime:
Textbox1.Attributes.Add("autocomplete", "off");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With