Jason,
Here's code (vb syntax) you can use at time of page render. Credit to Hugo
(don't know his last name) for posting the brains of this out here some
months ago.
In my example, I've created my own syntax for the substitution parameters,
e.g. any output in the format [#MyStringID] will be replaced with the
corresponding value from the message table (G.Message). The format is
arbitrary, just something you shouldn't normally encounter in your html
output. Nice part about this is that no matter how you create the [#...]--by
assigning it to label.text, inserting it literally into the html, etc.--it'll
be replaced. I also plan to introduce a similar syntax (e.g. [@...]) for
allowing macro substitution of globally available variables, e.g. SessionID,
UserName, etc.
hth,
Bill
Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
Dim ms As MemoryStream = New MemoryStream
Dim sw As StreamWriter = New StreamWriter(ms)
Dim htmlw As HtmlTextWriter = New HtmlTextWriter(sw)
MyBase.Render(htmlw)
htmlw.Flush()
ms.Position = 0
Dim reader As TextReader = New StreamReader(ms)
' This is where we can replace the output. DoSubParms handles the
' substitution parameters.
Response.Write(DoSubParms(reader.ReadToEnd))
End Sub
Private Const strRegexLiteral As String = "\[#\w+\]"
Public Function DoSubParms(ByVal HTMLInput As String) As String
' Go get the literals. Use regex to get keyword from [#keyword]
Dim rx As New Regex(strRegexLiteral)
Dim mc As MatchCollection = rx.Matches(HTMLInput)
Dim htmlOutput As String
htmlOutput = Regex.Replace(HTMLInput, strRegexLiteral, AddressOf
MatchReplace)
Return htmlOutput
End Function
Private Function MatchReplace(ByVal m As Match) As String
' This code is called for each match we find, so here's where we do
the
' actual substitution.
' Strip the keyword/type out of the match. The gist is that we know
we got
' here in the format [#...] or [@...], so the type is always at the
first
' position and the keyword is everything after that up to the final
' bracket.
Dim strKeyType As String = m.Value.Substring(1, 1)
Dim strKeyWord As String = m.Value.Substring(2, m.Value.Length - 3)
Dim strReturn As String = ""
Select Case strKeyType
Case "#" ' Literal
' Use keyword to get from G.Message
strReturn = G.Message(strKeyWord)
End Select
' Any errors above will result in empty string, so just return m.value
' unchanged
If strReturn = String.Empty Then
strReturn = m.Value
End If
Return strReturn
End Function