Home .NETASP.Net ASP.NET Using Different Ways to Disable Auto-Fill in Browser For a TextBox

    AutofillAll major browsers (Google chrome, Mozilla firefox, Safari and Internet explorer) has default functionality of auto complete the textboxes values means when user start to type value in textbox gets a dropdown like prefilled values in that particular textbox . We can disable the auto fill settings of browsers by uncheck the enable autofill option in browsers settings.

Implementation:

We have three ways to disable the autofill.
Method 1:
We can disable it by set autocomplete=”off” the in the form tag. It will disable it for entire form.
<form autocomplete=”off” id=”form1″ runat=”server”>

Method 2:
To disable autofill for a particular textbox set autocomplete=”off”
<asp:TextBox ID=”txtname” runat=”server” autocomplete=”off”></asp:TextBox>

Method 3:
We can also disable the autofill from code behind.
txtaddress.Attributes.Add(“autocomplete”, “off”);

Example:-
HTML Markup:
<form autocomplete=”off” id=”form1″ runat=”server”>
<div>
<table>
<tr><td>Name :</td><td><asp:TextBox ID=”txtname” runat=”server” autocomplete=”off”></asp:TextBox></td></tr>
<tr><td>Address :</td><td><asp:TextBox ID=”txtaddress” runat=”server” autocomplete=”off”></asp:TextBox></td></tr>
<tr><td>Salary :</td><td><asp:TextBox ID=”txtsalary” runat=”server” autocomplete=”off”></asp:TextBox></td></tr>
<tr><td></td><td><asp:Button ID=”btnsave” runat=”server” Text=”Save” /></td></tr>
</table>
</div>
</form>

Write the below given code on page load event.
C#:
protected void Page_Load(object sender, EventArgs e)
{
txtaddress.Attributes.Add(“autocomplete”, “off”);
txtsalary.Attributes.Add(“autocomplete”, “off”);
}

VB:
Protected Sub Page_Load(sender As Object, e As EventArgs)
txtaddress.Attributes.Add(“autocomplete”, “off”)
txtsalary.Attributes.Add(“autocomplete”, “off”)
End Sub

You may also like

Leave a Comment