124

How do I put a hint/placeholder inside a asp:TextBox? When I say a hint I mean some text which disappears when the user clicks on it. Is there a way to achieve the same using html / css?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
user590849
  • 11,655
  • 27
  • 84
  • 125
  • 2
    Which browsers are you supporting? HTML5 browsers support the `placeholder` attribute for textboxes. – rikitikitik Apr 05 '13 at 00:32
  • http://stackoverflow.com/questions/35501114/in-asp-net-unable-to-save-textboxes-if-the-text-is-given-in-between-and – Sachin Feb 19 '16 at 08:58

5 Answers5

236

The placeholder attribute

You're looking for the placeholder attribute. Use it like any other attribute inside your ASP.net control:

<asp:textbox id="txtWithHint" placeholder="hint" runat="server"/>

Don't bother about your IDE (i.e. Visual Studio) maybe not knowing the attribute. Attributes which are not registered with ASP.net are passed through and rendered as is. So the above code (basically) renders to:

<input type="text" placeholder="hint"/>

Using placeholder in resources

A fine way of applying the hint to the control is using resources. This way you may have localized hints. Let's say you have an index.aspx file, your App_LocalResources/index.aspx.resx file contains

<data name="WithHint.placeholder">
    <value>hint</value>
</data>

and your control looks like

<asp:textbox id="txtWithHint" meta:resourcekey="WithHint" runat="server"/>

the rendered result will look the same as the one in the chapter above.

Add attribute in code behind

Like any other attribute you can add the placeholder to the AttributeCollection:

txtWithHint.Attributes.Add("placeholder", "hint");
Linus Caldwell
  • 10,908
  • 12
  • 46
  • 58
61

Just write like this:

<asp:TextBox ID="TextBox1" runat="server" placeholder="hi test"></asp:TextBox>
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
pathak tejpal
  • 838
  • 1
  • 7
  • 13
19
 <asp:TextBox runat="server" ID="txtPassword" placeholder="Password">

This will work you might some time feel that it is not working due to Intellisence not showing placeholder

Shrivallabh
  • 2,863
  • 2
  • 27
  • 47
8

Adding placeholder attributes from code-behind:

txtFilterTerm.Attributes.Add("placeholder", "Filter" + Filter.Name);

Or

txtFilterTerm.Attributes["placeholder"] = "Filter" + Filter.Name;

Adding placeholder attributes from aspx Page

<asp:TextBox type="text" runat="server" id="txtFilterTerm" placeholder="Filter" />

Or

<input type="text" id="txtFilterTerm" placeholder="Filter"/>
Shibu Thomas
  • 3,148
  • 2
  • 24
  • 26
0
asp:TextBox ID="txtName" placeholder="any text here"
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Ahmed Soliman
  • 416
  • 5
  • 11