Friday, November 14, 2008

Autocomplete in ASP.NET TextBox Gotchas

When creating a textBox that uses Automplete behaviour with e.g. ASP.NET Ajax AutoCompleteExtender, you probably want to disable the browser's autocomplete feature to avoid that you get double autocompleters; one from the browser and one from the AutoCompleteExtender.

To do this in HTML, you add a autocomplete="off" attribute to your input box. But how to do this when we are working with ASP.NET:s TextBox control?

The ASP.NET TextBox control has a AutoCompleteType property that can control the autocomplete behaviour:

<asp:TextBox AutoCompleteType="AutoCompleteType" />

The problem with this is that it does not work in FireFox. It generates a autocomplete="off" attribute to the input element in IE, but when using FireFox, there is no autocomplete behaviour at all.

The solution to the problem is surprisingly simple (but not very well documented at MSDN):

<asp:TextBox autocomplete="off" />

Just put the autocomplete attribute in the asp:Textbox element. This will work since according to MSDN: "Any attribute you add to a control that does not map to a property of that control is passed through to the browser."

No comments: