Html 如何在 asp:textbox 中添加提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15823983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I put hint in a asp:textbox
提问by user590849
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:TextBox 中放置提示/占位符?当我说一个提示时,我的意思是一些文本,当用户点击它时会消失。有没有办法使用 html/css 实现相同的目标?
回答by Linus Caldwell
The placeholder
attribute
该placeholder
属性
You're looking for the placeholder
attribute. Use it like any other attribute inside your ASP.net control:
您正在寻找placeholder
属性。像 ASP.net 控件中的任何其他属性一样使用它:
<asp:textbox id="txtWithHint" placeholder="hint" runat="server"/>
Don't bother about your IDE (i.e. Visual Studio) maybe not knowingthe 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
placeholder
在资源中使用
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.aspxfile, your App_LocalResources/index.aspx.resxfile contains
将提示应用于控件的一个好方法是使用资源。通过这种方式,您可能有本地化的提示。假设您有一个index.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
:
像任何其他属性一样,您可以将 添加placeholder
到AttributeCollection
:
txtWithHint.Attributes.Add("placeholder", "hint");
回答by pathak tejpal
Just write like this:
就这样写:
<asp:TextBox ID="TextBox1" runat="server" placeholder="hi test"></asp:TextBox>
回答by Shrivallabh
<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
这会起作用,您可能有一段时间觉得它不起作用,因为 Intellisence 未显示占位符
回答by Shibu Thomas
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"/>
回答by Ahmed Soliman Flasha
asp:TextBox ID="txtName" placeholder="any text here"