C# 我可以在 ASP:Button 的 Text 属性中放置一个 <span> 标签吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/611449/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:14:41  来源:igfitidea点击:

Can I put a <span> tag in the Text property of an ASP:Button?

c#asp.net

提问by Xaisoft

I am trying to do something like this:

我正在尝试做这样的事情:

<asp:Button ID="btnSearch" 
            runat="server" 
            CssClass="greybtn" 
            Text='<span>Search</span>'
            OnClick="btnSearch_Click" />

It displays <span>Search</span>instead of just Search.

它显示<span>Search</span>而不仅仅是Search.

采纳答案by Rex M

You can't put markup inside a button. An asp:Button control just renders as an input button HTML tag: <input type="button" value="<span>Search</span>" />(technically value="&lt;span&gt;Search&lt;/span&gt;" />). The browser treats the contents of the valueattribute as a literal string.

您不能在按钮内放置标记。asp:Button 控件只是呈现为输入按钮 HTML 标记:(<input type="button" value="<span>Search</span>" />技术上value="&lt;span&gt;Search&lt;/span&gt;" />)。浏览器将value属性的内容视为文字字符串。

However, you canput markup inside a <button><span>Search</span></button>(you can put quite a bit of HTML in there, including images). This questiontalks about building a control which emits the buttontag.

但是,您可以在 a 中放置标记<button><span>Search</span></button>(您可以在其中放置相当多的 HTML,包括图像)。这个问题讨论了构建一个发出button标签的控件。

回答by Pawel Krakowiak

No. It renders a tag, so whatever you put in the Text property gets rendered as that button's value.

不。它呈现一个标签,所以你在 Text 属性中放置的任何内容都会呈现为该按钮的值。

回答by Joel Coehoorn

The text property will automatically turn <and >into the entities &lt;and &gt;. More than that, button renders to html as an inputelement, with the text set in it's valueproperty.

text 属性会自动变成<>变成实体&lt;&gt;。更重要的是,按钮呈现为 html 作为input元素,并在其value属性中设置文本。

Do you maybe want a HyperLink or Label control instead?

您可能想要一个 HyperLink 或 Label 控件吗?

回答by Rasith

You can have a asp linkButton for this:

您可以为此使用一个 asp linkBut​​ton:

回答by Philip Johnson

You can put a span on an ASP.NET LinkButton like this:-

您可以像这样在 ASP.NET LinkBut​​ton 上放置一个跨度:-

<asp:LinkButton ID="TestLinkButton" CssClass="btn btn-success" runat="server"><span class="glyphicon glyphicon-refresh"/>Press Me</asp:LinkButton>