2

Adding a placeholder in a textbox shows as a warning in Visual Studio

<asp:TextBox ID="name" CssClass="form-control" runat="server" placeholder="e.g. John Doe" ></asp:TextBox>

Error message

Attribute 'placeholder' is not a valid attribute of element 'TextBox'.  

What is the recommanded way to put a placeholder in a textbox and thus fix the warning

user2650277
  • 6,289
  • 17
  • 63
  • 132

2 Answers2

3

Don't worry about that warning message. Your place holder will work exactly it supposed to be. This happens because of the ASP .Net version support for HTML. As .NET framework 4.5 introduced many new features supporting HTML5, and for all those unsupported attributes it would display the same unsupported warning.

Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
  • If one finds Visual Studio's message annoying, then the placeholder can alternatively be added in the Code Behind like `name.Attributes.Add("placeholder", "e.g. John Doe")`. Putting it in the Code Behind is kind of annoying too, but at least the message ceases. See https://stackoverflow.com/a/15824040/2615878 for a few more details. – Mike Grove aka Theophilus Sep 15 '17 at 13:23
2

Placeholder is an HTML5 attribute. That's why you are getting a warning. The placeholder attribute will only work in modern browsers like Chrome, IE11 and latest version of Firefox. But in older browsers it will not work. To make placeholder work in other browsers you need to use AJAX toolkit TextBoxWatermarkExtender. Example:

 <asp:TextBox ID="name" CssClass="form-control" runat="server" placeholder="e.g. John Doe" ></asp:TextBox>

You can also write it like this:

<asp:TextBox ID="name" CssClass="form-control" runat="server" value="Name" onblur="if(value=='') value = 'e.g. John Doe'" onfocus="if(value=='e.g. John Doe') value = ''"></asp:TextBox>
madster
  • 23
  • 2
  • 6
Rajesh
  • 208
  • 1
  • 4