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?
如何在一个asp:文本框中添加提示/占位符?当我说一个提示时,我指的是当用户点击它时消失的一些文本。是否有一种方法可以使用html / css实现同样的功能?
5 个解决方案
#1
145
The placeholder
attribute
You're looking for the placeholder
attribute. Use it like any other attribute inside your ASP.net control:
您正在寻找占位符属性。像ASP.net控件中的其他属性一样使用它:
<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:
不要担心你的IDE(比如Visual Studio)可能不知道这个属性。未在ASP.net中注册的属性将被传递并按原样呈现。因此上面的代码(基本上)呈现为:
<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
将提示应用到控件的一个好方法是使用资源。这样,您可能有本地化的提示。假设你有一个索引。aspx文件,你的App_LocalResources / index.aspx。resx文件包含
<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
:
和其他属性一样,您可以向AttributeCollection添加占位符:
txtWithHint.Attributes.Add("placeholder", "hint");
#2
59
Just write like this:
这样写:
<asp:TextBox ID="TextBox1" runat="server" placeholder="hi test"></asp:TextBox>
#3
15
<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
这将会起作用,您可能会有一段时间觉得它不起作用是因为理解不显示占位符
#4
4
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
从aspx页面添加占位符属性
<asp:TextBox type="text" runat="server" id="txtFilterTerm" placeholder="Filter" />
Or
或
<input type="text" id="txtFilterTerm" placeholder="Filter"/>
#5
0
asp:TextBox ID="txtName" placeholder="any text here"
#1
145
The placeholder
attribute
You're looking for the placeholder
attribute. Use it like any other attribute inside your ASP.net control:
您正在寻找占位符属性。像ASP.net控件中的其他属性一样使用它:
<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:
不要担心你的IDE(比如Visual Studio)可能不知道这个属性。未在ASP.net中注册的属性将被传递并按原样呈现。因此上面的代码(基本上)呈现为:
<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
将提示应用到控件的一个好方法是使用资源。这样,您可能有本地化的提示。假设你有一个索引。aspx文件,你的App_LocalResources / index.aspx。resx文件包含
<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
:
和其他属性一样,您可以向AttributeCollection添加占位符:
txtWithHint.Attributes.Add("placeholder", "hint");
#2
59
Just write like this:
这样写:
<asp:TextBox ID="TextBox1" runat="server" placeholder="hi test"></asp:TextBox>
#3
15
<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
这将会起作用,您可能会有一段时间觉得它不起作用是因为理解不显示占位符
#4
4
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
从aspx页面添加占位符属性
<asp:TextBox type="text" runat="server" id="txtFilterTerm" placeholder="Filter" />
Or
或
<input type="text" id="txtFilterTerm" placeholder="Filter"/>
#5
0
asp:TextBox ID="txtName" placeholder="any text here"