Sunday, August 24, 2008

Disabling Auto-Complete on ASP.NET Forms

Popular browsers, such as Internet Explorer and Firefox support something called Auto-Complete. You've seen this many times. You go to a online form and as you start to type in fields you get a drop-down showing values you've typed in that field before. This feature can be turned off, but it really is a useful feature and can save you a lot of typing when entering redundant values.

As a web developer, you have to be conscious of the fact that the user's browser will likely have auto-complete turned on and be responsible enough to act accordingly. If you have a form that where the user could possibly enter private or secure values you need to be mindful that if the user is on a public computer that these values will be cached by the browser and seen by other users of that computer. You are able to control if the browser uses AutoComplete for your form or for specific values with just a small & simple tweak.
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");

From Ryan Farley 's weblog. See the original post on his blog : http://ryanfarley.com/blog/archive/2005/02/23/1739.aspx.

13 comments:

Anonymous said...

Hmmmm.... maybe just to give credit where credit is actually due it should be mentioned that the original post/poster is here:

http://ryanfarley.com/blog/archive/2005/02/23/1739.aspx

Masoud Tabatabaei said...

You are right Man. Done.

Anonymous said...

thanks! It was very helpful

Anonymous said...

It was helpful... Thanks.

Neelam said...
This comment has been removed by the author.
Anonymous said...

thanks Itz Very Useful
It solved autocomplete problem easily

Neelam said...

itz best way to solve autocomlete problem

Anonymous said...

thank you ... !
you solved my problam :)

Anonymous said...

salam !
mamnon am .
moshkelamo hal kardid :D

( i think your iranian and speak persian ... :D)

have a good day ! :)

Anonymous said...

And why, may I ask, did you copy a whole blog post from someone else? Referencing back to them doesn't make it right.

Anonymous said...

Gracias.. Es justo lo que buscaba...;) ..... Att KillerCat

Lenin Moreno Penna said...

gracias!

lingmaaki said...

A simple...C# Autocomplete textbox

Ling