How do I put hint in a asp:textbox
Asked Answered
M

5

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?

Migration answered 5/4, 2013 at 0:25 Comment(2)
Which browsers are you supporting? HTML5 browsers support the placeholder attribute for textboxes.Motmot
#35501614Quentinquercetin
G
239

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");
Grooved answered 5/4, 2013 at 0:32 Comment(3)
For some reason the resource trick "WithHint.placeholder" didn't work for me.Merriman
When using resources, you can do this for a local resource file: <asp:TextBox ID="txtWithHint" runat="server" placeholder="<%$ Resources: 52 %>" /> or for global resources: <asp:TextBox ID="txtWithHint" runat="server" placeholder="<%$ Resources: ResourceFile, ResourceValue %>" />Armitage
when adding a control in code, all you apparently need is: myTextBox.Attributes.Add("placeholder", "hint");Sphalerite
B
61

Just write like this:

<asp:TextBox ID="TextBox1" runat="server" placeholder="hi test"></asp:TextBox>
Beautiful answered 5/4, 2013 at 0:34 Comment(0)
N
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

Nerland answered 18/2, 2015 at 11:23 Comment(0)
E
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"/>
Exodus answered 8/1, 2016 at 5:36 Comment(0)
S
0
asp:TextBox ID="txtName" placeholder="any text here"
Starlet answered 19/11, 2015 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.