0

I want to add a HtmlAnchor to the .ascx control. So far I have the code like this:

private void SetPhoneNumber()
    {
        HtmlAnchor htmlAnchor = new HtmlAnchor();
        const string spanTag = @"<span class=""icon phone"">m</span>";
        string anchor = spanTag + Context.CurrentPhoneNumber();
        htmlAnchor.InnerText = anchor;
        Controls.Add(htmlAnchor);
    }

This is not solving my purpose as its showing like this:

Instead of span it needs to show a phone icon.

When it should be rendered in the HTML, it should look like this:

 <a href="tel:888.444.4444" class="phone"><span class="icon phone">m </span>888.444.4444</a>

Can anyone help me out on this?

Coder
  • 83
  • 1
  • 2
  • 8
  • As a side question - why not do an actual control template with <%= %> avoiding String.Format and potentially messy hardcoded strings, dealing with obscure html encoding? – rgripper Apr 25 '13 at 20:32

2 Answers2

1

Set the InnerHtml of the anchor tag:

HtmlAnchor htmlAnchor = new HtmlAnchor();
const string spanTag = @"<span class=""icon phone"">m</span>";
string anchor = spanTag + Context.CurrentPhoneNumber();
htmlAnchor.InnerHtml = anchor;
Controls.Add(htmlAnchor);
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
1

change innertext to innerhtml

 private void SetPhoneNumber()
{
    HtmlAnchor htmlAnchor = new HtmlAnchor();
    const string spanTag = @"<span class=""icon phone"">m</span>";
    string anchor = spanTag + Context.CurrentPhoneNumber();
    htmlAnchor.InnerHtml = anchor;
    Controls.Add(htmlAnchor);
}
Shafqat Masood
  • 2,532
  • 1
  • 17
  • 23