10

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.

Rex M
  • 142,167
  • 33
  • 283
  • 313
Xaisoft
  • 45,655
  • 87
  • 279
  • 432

5 Answers5

14

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

<asp:LinkButton ID="TestLinkButton" CssClass="btn btn-success" runat="server"><span class="glyphicon glyphicon-refresh"/>Press Me</asp:LinkButton>
Philip Johnson
  • 1,091
  • 10
  • 24
  • Best answer in my opinion. Is there any reason to use a Button over a LinkButton? Or essentially, any unwanted side effects from doing it this way? – h0r53 Oct 07 '16 at 16:44
  • Sorry only just spotted this comment for some reason. A link button and a button are two different things. link button generates an "a" tag, a button is an "input" tag which is usually used for submitting forms (although ASP.NET webforms hides this a bit, you can click them and call click events in webforms). This is really another question so I'm not going to go on. – Philip Johnson May 16 '17 at 20:55
5

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 value attribute as a literal string.

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

Community
  • 1
  • 1
Rex M
  • 142,167
  • 33
  • 283
  • 313
2

You can have a asp linkButton for this:

Rasith
  • 21
  • 3
0

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

Pawel Krakowiak
  • 9,940
  • 3
  • 37
  • 55
0

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

Do you maybe want a HyperLink or Label control instead?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794