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
  • 135,205
  • 29
  • 270
  • 310
Xaisoft
  • 42,877
  • 83
  • 270
  • 415

5 Answers5

13

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
  • 957
  • 9
  • 20
  • 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
  • 135,205
  • 29
  • 270
  • 310
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,300
  • 2
  • 33
  • 54
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
  • 362,140
  • 107
  • 528
  • 764