Home Search Member List Faq Register Login  
UltimateSearch
How to add ANY icon to the head of ANY links in search results

Thread Starter: MADCookie   Started: 09-03-2008 2:05 PM   Replies: 0
 Karamasoft Support Forums » General Discussions » UltimateSearch » How to add ANY icon to the head of ANY links in search results
 Printable Version    « Previous Thread   Next Thread »
  03 Sep 2008, 2:05 PM
MADCookie is not online. Last active: 9/10/2009 3:36:53 PM MADCookie

Top 10 Posts
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


  
 Page 1 of 1 (1 items)
Karamasoft Support Forums » General Discussions » UltimateSearch » How to add ANY icon to the head of ANY links in search results

You can add attachments
You can post new topics
You can reply to topics
You can delete your posts
You can edit your posts
You can create polls
You can vote in polls
Forum statistics are enabled
Forum is unmoderated

© 2002-2010 Karamasoft LLC. All rights reserved.