Created
December 22, 2009 16:20
-
-
Save SamWM/261828 to your computer and use it in GitHub Desktop.
TruncatedHyperLink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Create a hyperlink that is truncated if the text is too long | |
/// </summary> | |
/// <param name="url">Page to link to</param> | |
/// <param name="text">Text to show</param> | |
/// <param name="length">Truncate text at this length</param> | |
/// <returns>An anchor, truncated if appropriate</returns> | |
public static string TruncatedHyperLink(object url, object text, int length) | |
{ | |
if (url == DBNull.Value || text == DBNull.Value) return String.Empty; | |
return TruncatedHyperLink(url.ToString(), text.ToString(), length); | |
} | |
/// <summary> | |
/// Create a hyperlink that is truncated if the text is too long | |
/// </summary> | |
/// <param name="url">Page to link to</param> | |
/// <param name="text">Text to show</param> | |
/// <param name="length">Truncate text at this length</param> | |
/// <returns>An anchor, truncated if appropriate</returns> | |
public static string TruncatedHyperLink(string url, string text, int length) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendFormat("<a href=\"{0}\"", url); | |
if (text.Length > length) | |
{ | |
sb.AppendFormat(" title=\"{0}\"", text); | |
} | |
sb.Append(">"); // end open anchor | |
if (text.Length > length) | |
{ | |
sb.AppendFormat("{0}...", text.Substring(0, length)); | |
} | |
else | |
{ | |
sb.Append(text); | |
} | |
sb.Append("</a>"); | |
return sb.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment