如何创建和输出数据作为链接?

时间:2014-10-16 14:23:33

标签: c# asp.net

我创建了一个Web表单,我可以使用文本编辑器将文章插入数据库。我的文章表有以下字段名; id,title,content,type和uploadDate。内容' fieldname由文本组成。此文本还涉及另一个数据库表中的数据,称为标记(即tag_id,名称,类型),它不链接到文章表。

当我查看webform上的文章时,它们看起来像以下输出:

Pellentesque eget elit tag1. Vivamus ut lacinia lectus. Mauris eu efficitur mi. 
Nam blandit est ut justo scelerisque, in sagittis nibh scelerisque. 
tag2 placerat id risus ut aliquam. Vestibulum maximus rutrum arcu et sagittis.

我的问题是,如何让我的文章内容看起来像下面所需的输出:

Pellentesque eget elit ***tag1***. Vivamus ut lacinia lectus. Mauris eu efficitur mi. 
Duis placerat id risus ut aliquam. Vestibulum maximus rutrum arcu et sagittis.  
***tag2*** id consequat dui. Suspendisse non imperdiet odio.

在文章内容中显示,标记名称(tag1& tag2)作为链接。链接将由某种查询字符串组成,当点击时,它会呈现给一个网页,该网页保存该标记的id记录(通过DataTable)。

我知道,这种方法需要我为标签的名称创建超链接,但我不太确定,我将如何动态创建(即设置[t​​ag1的所有实例, tag2] as hyperlinks)。我是否需要在文章的插入方法或我的aricle的getData()方法中创建此任务?

非常感谢任何帮助。非常感谢

1 个答案:

答案 0 :(得分:1)

我有点不确定您是否可以使用文本框中可点击的链接进行编辑。我假设没有,这是现有文章的视图页面。

如果是这种情况,有很多方法可以做到这一点,我个人建议使用HtmlGenericControl

protected override void OnPreRender(EventArgs e)
{     
    var ctrl = new HtmlGenericControl("div");
    string html = this.GetArticleText();

    // create the html formatted HTML/Links taking into account anti-xss attacks
    foreach(var tag in this.LoadAllTags())
    {
      html.Replace(tag.Key, this.CreateLinkHtml(tag);
    }

    ctrl.InnerHtml = html;

    this.SomePageContainer.Controls.Add(ctrl);

    base.OnPreRender(e);
}
相关问题