18

What is the step to get example text to show up in an asp.net textbox.

For example, textbox w/ ID = "textboxDate" has [mm/dd/yyyy] inside it for the user to reference.

Nick
  • 882
  • 2
  • 9
  • 31
  • possible duplicate of [How do I put hint in a asp:textbox](http://stackoverflow.com/questions/15823983/how-do-i-put-hint-in-a-asptextbox) – Tim Jan 08 '14 at 21:10

4 Answers4

38

I believe you want a placeholder attribute:

<asp:TextBox ID="placeholderTextBox" runat="server" placeholder="mm/dd/yyyy"></asp:TextBox>
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • 1
    I have a question. Please answer. I used this `attribute` for `TextBox` but Visual Studio said : `Attribute 'placeholder' is not a valid attribute of element 'TextBox'` But program worked fine , I could get result what I wanted. Can this message be cause any problem ? – Jeyhun Jun 28 '14 at 13:55
  • 1
    @Jeyhun, don't worry about that warning message. – Nick Oct 14 '14 at 22:43
  • @Jeyhun, the message is merely an annoyance. It can be avoided by setting the placeholder in the Code Behind like `placeholderTextBox.Attributes.Add("placeholder", "mm/dd/yyyy")`. Putting it in the Code Behind is kind of annoying too, but at least the message stops. (This is true for Visual Studio 2015 and possibly other versions.) – Mike Grove aka Theophilus Sep 15 '17 at 13:31
6

but placeholder doenst work for many IE browser because placeholder is html5 thing.

try to use modernizr framework. it works for all browsers including all IE

here is a sample code for you.

if(modernizr.input.placeholder) {
   //insert placeholder polyfill script here. 
}
Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189
3

Visual Studio maybe not knowing the attribute. Attributes which are not registered with ASP.net are passed through and rendered as is.

<asp:TextBox ID="TextBox1" runat="server"  Width="187px" placeholder="mm/dd/yyyy"></asp:TextBox>

So the above code (basically) renders to:

<input type="text" placeholder="mm/dd/yyyy"/>
Sohail Qureshi
  • 689
  • 11
  • 24
2

You can allways use:

<input ID="placeholderTextBox" type="text" runat="server" placeholder="mm/dd/yyyy" />

and on code behind you can get or set the value using

Dim myValue As String = placeholderTextBox.value or placeholderTextBox.value = "whatsoever"
Vicedriver
  • 41
  • 3