03 Sep 2008, 2:05 PM
|
MADCookie
Joined on 05-30-2008
Posts 49
|
How to add ANY icon to the head of ANY links in search results
|
|
|
|
Thanks to this posting, I took Karamasoft's work and added a little bit more. Sorry my code isn't as colorful and pretty as Karamasoft's posted code.
How to add a PDF icon to the head of PDF links in search results
The code will allow you to assign icons to all your links in your search results. The icon can be before or after the link.
To use it... _extensionsAndIconPaths is a dictionary of the extensions you want to have an icon display and the path to the icon. You create .Add statements for each extension you want to support. Here you see I have three: Word DOC, Excel XLS, and Adobe PDF. You can use a period or not when listing the extensions.
Call addIconToResults in the ItemBound event to make the icon appear. This method is overloaded so you can display the icon in front of or behind the hyperlinked title of the search result. The default behavior is to display in front of the title.
.aspx.vb:
Private _extensionsAndIconPaths As New StringDictionary()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load With _extensionsAndIconPaths .Add("doc", "/doc.gif") .Add("xls", "/xls.gif") .Add("pdf", "/pdf.gif") End With End Sub
Private Sub UltimateSearchOutput1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles UltimateSearchOutput1.ItemDataBound
addIconToResults(e, _extensionsAndIconPaths) ' addIconToResults(e, _extensionsAndIconPaths, False) End Sub
Private Sub addIconToResults(ByVal e As System.Web.UI.WebControls.DataListItemEventArgs, ByVal extensionsAndIconPaths As StringDictionary) addIconToResults(e, extensionsAndIconPaths, True) End Sub Private Sub addIconToResults(ByVal e As System.Web.UI.WebControls.DataListItemEventArgs, ByVal extensionsAndIconPaths As StringDictionary, ByVal iconBeforeLinkedText As Boolean) If ((e.Item.ItemType = ListItemType.Item) OrElse (e.Item.ItemType = ListItemType.AlternatingItem)) Then Dim literalCast As Literal = TryCast(e.Item.Controls(0), Literal) If literalCast IsNot Nothing Then Dim oldText As String = literalCast.Text For Each extension As String In extensionsAndIconPaths.Keys() If oldText.IndexOf(addDotToExtension(extension), System.StringComparison.OrdinalIgnoreCase) <> -1 Then Dim startInsertPosition As Integer = getPositionIndex(iconBeforeLinkedText, oldText) Dim newText As String = String.Format("{0}<img src=""{2}"" style=""vertical-align:middle;"" alt="""" /> {1}", oldText.Substring(0, startInsertPosition), oldText.Substring(startInsertPosition), extensionsAndIconPaths.Item(extension)) literalCast.Text = newText End If Next End If End If End Sub
Private Function getPositionIndex(ByVal iconBeforeLinkedText As Boolean, ByVal oldText As String) As Integer Dim results As Integer = 0 Select Case iconBeforeLinkedText Case True results = oldText.IndexOf("<a href=") Case False results = oldText.IndexOf("</a>") + 4 End Select Return results End Function
Private Function addDotToExtension(ByVal extensionValue As String) As String Dim results As String = extensionValue If Not extensionValue.StartsWith(".") Then results = String.Concat(".", extensionValue) Return results End Function
|
|
|
|
|
|
|
|
|