Sunday, September 20, 2015

Asp.net: Different ways to disable auto-fill in browser for a textbox

In this article I am going to explain different ways to disable auto-fill for a textbox in asp.net

Description:

All 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.

Asp.net: Different ways to disable auto-fill in browser for a textbox


This is manual option that will be performed by the each user on their browsers. But asp.net has option if we implement that, no need to make changes 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

Build and run the project. To test it insert the record via click on submit button. After that again type the same value as you enter earlier.

   In this article we have learn to how we can disable the auto-fill in browser for a textbox asp.net using C# and VB.netI hope you enjoyed this article. 

1 comment:

  1. Good Article but This does not work for chrome. (version 47)

    ReplyDelete