Question

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?

Was it helpful?

Solution

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);

OTHER TIPS

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top