How to Disable/Turn off browser autocomplete feature in TextBox in asp.net

Introduction: In this article i am going to explain How to disable, stop or turn off the auto complete or auto fill feature in input controls e.g. TextBox controls in asp.net using both C# and VB.Net languages.In previous articles i explained How to Retain password value in the asp.net TextBox after postback event and Count and display remaining characters in the multiline textbox and Ajax AutoCompleteExtender control example and Get Title,Description and Keywords Meta tags from URL and Get browser name,version,type, operating system and Show tool tip message using CSS and HTML. 

Disable auto complete in asp.net textbox controls


Description: All the major browsers such as Google Chrome , Mozilla Firefox and Internet Explorer etc has a default Auto complete feature that stores the filled information in browser cache and show that previously filled data when we start typing again in those fields e.g. TextBox controls in asp.net web pages. 

This default feature of browsers actually helps user while filling the data on the forms where same information is to be filled again and again. But if the form has the fields for entering confidential or sensitive information e.g. bank account number or Credit Card Number etc then this feature should be turned off.

Let's say if the user fills up an online form on a public computer e.g. in cyber cafe then the values enter by him will be cached by the browser and will be visible to other users of that public computer. So in that case it is always recommended to developers to turn this feature off in this situation.

There are 3 ways to turn off this feature that is listed below. Just pick any one of these:

1) Setting the autocomplete="off" in the Form tag to disable autocomplete on entire form as:
<form id="form1" method="post" runat="server" autocomplete="off">

2) Setting the autocomplete="off" in all the Textbox controls where you want to disable autocomplete as:
<asp:TextBox ID="txtName" runat="server" autocomplete="off"></asp:TextBox>
<asp:TextBox ID="txtContact" runat="server" autocomplete="off"></asp:TextBox>
<asp:TextBox ID="txtBankAccountNo" runat="server" autocomplete="off"></asp:TextBox>
<asp:TextBox ID="txtCreditCardNo" runat="server" autocomplete="off"></asp:TextBox>

3) At run time turning this feature off via the attributes of the controls in the code behind as:
        txtName.Attributes.Add("autocomplete", "off");
        txtContact.Attributes.Add("autocomplete", "off");
        txtBankAccountNo.Attributes.Add("autocomplete", "off");       
        txtCreditCardNo.Attributes.Add("autocomplete", "off");

Implementation: let's create an asp.net example web application to see it in action. 
  • In the design page (.aspx) design the form as: 
<form id="form1" method="post" runat="server" autocomplete="off">
    <div>
    <fieldset style="width:300px;">
    <legend>Disable Autocomplete in TextBox example</legend>
        <table>
    <tr>
    <td>Name: </td>
    <td>
        <asp:TextBox ID="txtName" runat="server" autocomplete="off"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Contact No.: </td>
    <td>
        <asp:TextBox ID="txtContact" runat="server" autocomplete="off"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Bank Acccount No.: </td>
    <td>
        <asp:TextBox ID="txtBankAccountNo" runat="server" autocomplete="off"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Credit Card No.: </td>
    <td>
        <asp:TextBox ID="txtCreditCardNo" runat="server" autocomplete="off"></asp:TextBox></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" /></td>
    </tr> 
    </table>
    </fieldset>
    </div>
    </form>
  
Note: I have demonstrated all the 3 methods to turn off the auto complete feature in the example below. You just need to use any one of these methods.


C#.Net Code to disable Auto complete featurein asp.net textbox
  • In the code behind file (.aspx.cs) write the code on page load event as:
protected void Page_Load(object sender, EventArgs e)
    {
        txtName.Attributes.Add("autocomplete", "off");
        txtContact.Attributes.Add("autocomplete", "off");
        txtBankAccountNo.Attributes.Add("autocomplete", "off");       
        txtCreditCardNo.Attributes.Add("autocomplete", "off");
    }


VB.Net Code to disable Auto complete feature in asp.net textbox
  • In the code behind file (.aspx.vb) write the code on page load event as:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtName.Attributes.Add("autocomplete", "off")
        txtContact.Attributes.Add("autocomplete", "off")
        txtBankAccountNo.Attributes.Add("autocomplete", "off")
        txtCreditCardNo.Attributes.Add("autocomplete", "off")
    End Sub 
  • Now run the application and enter any record and click on submit. Refresh the browser by pressing F5 and again try to enter the same record and you will see previously filled data is not showing while entering.
 Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..