Summary
This article provides an explanation of the HtmlDocumentLinkInjector class included the CalzadaMedia.Web common library. For reference, an example based upon working code is also provided.
Overview
The HtmlDocumentLinkInjector class provides functionality to insert HTML Hyperlinks into an HTML document based upon matching texts. The principle use of the HtmlDocumentLinkInjector is to automatically insert links into multiple HTML documents without the need to actually specify the link within the document itself.
The HtmlDocumentLinkInjector simplifies overall link management. There is no need to manually add links to every HTML document. With the HtmlDocumentLinkInjector you define the link once and then run the HTML content through it.
How It Works
For example, you may wish to automatically link to your website's Privacy Policy every time the words Privacy Policy are used (see Code Example below). The HtmlDocumentLinkInjector can search through HTML document for all occurences of Privacy Policy and then insert a Hyperlink around this text to point towards the Url of your Privacy Policy.
The HtmlDocumentLinkInjector only inserts links if none already exist. If any matching text is found within an existing link, it is not processed.
Three Parameters
There are there parameters for each potential match: the match text, the Url and the tooltip.
- The match text is the text string against which the HtmlDocumentLinkInjector searches for. For example: Privacy Policy.
- The Url is the Url of the link to insert around any match.
- The Tooltip is entirely optional. If no tooltip is defined, then the match text is automatically used.
Code Example
Using the above example, the following code will add a hyperlink around every instance of "Privacy Policy" found within the value of htmlText.
Dim linkInjector As New CalzadaMedia.Web.UI.HtmlDocumentHyperlinkInjector
linkInjector.AddLink("http://mydomain.com/privacy-policy.html", "Our Privacy Policy")
htmlText = linkInjector.InjectLinks(htmlText)
linkInjector.Dispose()
Programming Notes
Adding Links
Links may be added individually using the AddLink
method or in multiple using the AddLinks
method. With AddLinks
, a System.Data.DataTable of link values must be supplied along with the relevant column names.
AddLink Method
Dim linkInjector As New CalzadaMedia.Web.UI.HtmlDocumentHyperlinkInjector
linkInjector.AddLink("http://mydomain.com/privacy-policy.html", "Our Privacy Policy")
htmlText = linkInjector.InjectLinks(htmlText)
linkInjector.Dispose()
AddLinks Method
In the code example below, the DataTable mylinksDataTable
contains the columns link_matchtext
, link_url
, and link_tooltip
respectively contain the values of the match text, the Url to insert and any tooltip to use.
Dim linkInjector As New CalzadaMedia.Web.UI.HtmlDocumentHyperlinkInjector
linkInjector.AddLinks(myLinksDataTable, "link_matchtext", "link_url", "link_tooltip")
htmlText = linkInjector.InjectLinks(htmlText)
linkInjector.Dispose()
Link Attributes
Additional Hyperlink attributes like class, rel and target may be specified using the LinkAttributes
property.
Please note: The attributes id or title may not be specified. They will be ignored if they are specified.
In the code example below, the target
attribute is specified.
Dim linkInjector As New CalzadaMedia.Web.UI.HtmlDocumentHyperlinkInjector
linkInjector.AddLink("http://mydomain.com/privacy-policy.html", "Our Privacy Policy")
linkInjector.Attributes.Add("target", "_blank")
htmlText = linkInjector.InjectLinks(htmlText)
linkInjector.Dispose()
Object Disposal
It is strongly recommended the Dispose()
method is called once you have finished with the HtmlDocumentLinkInjector