I had a look at the empty link url "problem". 2 Issues are at hand:
* the "read more..." link should be invisible/deleted when no link is provided
* if the title is linked, the title should not be invisible/deleted, it should just not be a link
I took a shot at solving this with a regular expression. My first attempt looked like this:
strContent = Regex.Replace(strContent, "<A\b[^>]*>(.*?)</A>", "$1", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
this looks for any string that looks like this: <a [...]>[innertext]</a>, and replaces it with just [innertext], in effect removing the hyperlink. For the title this is a fine solution, for the "read more..." it isn't, because i would like to delete "read more..." instead of just removing the link. This reveals another issue: "read more..." should be localized, because, as Jan already mentioned, if someone changes the template, it is no longer read from the resource file, and thus can no longer be localized. My solution for this is to add a new token [MORE], which can be localized, and also be used for generic deletion of the link in case it's empty, that is done like this:
strContent = Regex.Replace(strContent, "<A\b[^>]*>\[MORE\]</A>", "", RegexOptions.IgnoreCase OrRegexOptions.Singleline)
This will just delete any complete hyperlink that looks like <a ...>[MORE]</a>
Wrapping it up, my final lstAnnouncements_ItemDataBound looks like this now:
Private Sub lstAnnouncements_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles lstAnnouncements.ItemDataBound
Dim strContent As String = strTemplate
Dim strValue As String
Dim clearURL As Boolean
' add content to template
Dim objProperties As ArrayList = Common.Utilities.CBO.GetPropertyInfo(GetType(AnnouncementInfo))
Dim intProperty As Integer
Dim objPropertyInfo As PropertyInfo
For intProperty = 0 To objProperties.Count - 1
clearURL = False
objPropertyInfo = CType(objProperties(intProperty), PropertyInfo)
strValue = Server.HtmlDecode(DataBinder.Eval(e.Item.DataItem, objPropertyInfo.Name).ToString())
Select Case objPropertyInfo.Name.ToUpper
Case "URL"
If strValue <> "" Then
strValue = FormatURL(strValue, CType(DataBinder.Eval(e.Item.DataItem, "TrackClicks"), Boolean))
Else
clearURL = True
strValue = ""
End If
Case "CREATEDDATE"
strValue = CType(strValue, Date).ToLongDateString
Case "NEWWINDOW"
If CType(strValue, Boolean) Then
strValue = "_new"
Else
strValue = "_self"
End If
End Select
If Not strValue = "" Then
strContent = strContent.Replace("[" & objPropertyInfo.Name.ToUpper & "]", strValue)
End If
If clearURL Then
strContent = Regex.Replace(strContent, "<A\b[^>]*>\[MORE\]</A>", "", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
strContent = Regex.Replace(strContent, "<A\b[^>]*>(.*?)</A>", "$1", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
End If
Next intProperty
strContent = strContent.Replace("[MORE]", Localization.GetString("More.Text"))
' assign the content
Dim lblContent As Label = CType(e.Item.FindControl("lblContent"), Label)
lblContent.Text = strContent
End Sub
My first change to the Announcements module as (yet unofficial) Announcement Module Team Lead ;)
Cheers,
Erik