Monday 9 June 2008

How do I disable AutoComplete in ASP.NET?

Popular web browsers have a feature called AutoComplete which saves information that has previously been inputted. Web addresses, form data and username/passwords are usually recorded to make life easier in the future.

As a developer, you may want to disable this feature for some types of input including credit card information. ASP.NET has the ability to override the user's choice with the autocomplete attribute by setting it to off. This can be added to a form tag or an individual TextBox control.

Examples:

<form runat="server" autocomplete="off"></form>

<asp:TextBox ID="tbCreditCardNumber" runat="server" autocomplete="off" />

(Runtime)
tbCreditCardNumber.Attributes.Add( "autocomplete", "off" );

Alternatively, you can use the AutoCompleteType attribute on an individual TextBox control and set it to Disabled to achieve the same result.

Example:

<asp:TextBox ID="tbCreditCardNumber" runat="server" AutoCompleteType="Disabled" />

(Runtime)
tbCreditCardNumber.AutoCompleteType = AutoCompleteType.Disabled;

5 comments:

Anonymous said...

is it possible to turn on autocomplete for a text box while its form autocomplete set turned-off

Andrew Gunn said...

I don't think so. If you only want one control to have AutoComplete, disable it on all the others individually.

Anonymous said...

Thank you for posting this - saved me a lot of time trying to figure it out! No more worries about users leaving private info for others to discover.

Jason Smith said...

Thanx for this awesome example!
its good.
easy to use.
straight to the point

fanx a mil :D

Anonymous said...

Doesn't work in asp.net 2.0. The auto complete attribute disappear at runtime