从C#添加'HtmlAnchor'

时间:2013-04-25 20:25:33

标签: c# anchor htmlcontrols

我想在.ascx控件中添加一个HtmlAnchor。到目前为止,我有这样的代码:

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

这不能解决我的目的,因为它显示如下:

Instead of span it needs to show a phone icon.

当它应该在HTML中呈现时,它应该如下所示:

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

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

设置锚标记的InnerHtml

HtmlAnchor htmlAnchor = new HtmlAnchor();
const string spanTag = @"<span class=""icon phone"">m</span>";
string anchor = spanTag + Context.CurrentPhoneNumber();
htmlAnchor.InnerHtml = anchor;
Controls.Add(htmlAnchor);

答案 1 :(得分:1)

将innertext更改为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);
}