Hi RichardSa,
Check this example. Now please take its reference and correct your code.
HTML
<asp:Label ID="lblData" Text="" runat="server" />
<style type="text/css">
a:link {
text-decoration: none;
cursor: pointer;
color: blue;
}
</style>
Namespaces
C#
using System.Text.RegularExpressions;
VB.Net
Imports System.Text.RegularExpressions
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
lblData.Text = "Hello people please I have good news this morning, check it out on www.goodnews.com, it's one you don't want to miss";
lblData.Text = ConvertTextUrlToLink(lblData.Text);
}
public string ConvertTextUrlToLink(string rawString)
{
Regex linkParser = new Regex(@"\b(?:http://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (Match m in linkParser.Matches(rawString))
{
if (m.Value.IndexOf("http") != -1 || m.Value.IndexOf("https") != -1)
{
rawString = rawString.Replace(m.Value, string.Format("<a target='_blank' href = '{0}'>{0}</a>", m.Value));
}
else
{
rawString = rawString.Replace(m.Value, string.Format("<a target='_blank' href = 'http://{0}'>{0}</a>", m.Value));
}
}
return rawString;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
lblData.Text = "Hello people please I have good news this morning, check it out on www.goodnews.com, it's one you don't want to miss"
lblData.Text = ConvertTextUrlToLink(lblData.Text)
End Sub
Public Function ConvertTextUrlToLink(ByVal rawString As String) As String
Dim linkParser As Regex = New Regex("\b(?:http://|www\.)\S+\b", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
For Each m As Match In linkParser.Matches(rawString)
If m.Value.IndexOf("http") <> -1 OrElse m.Value.IndexOf("https") <> -1 Then
rawString = rawString.Replace(m.Value, String.Format("<a target='_blank' href = '{0}'>{0}</a>", m.Value))
Else
rawString = rawString.Replace(m.Value, String.Format("<a target='_blank' href = 'http://{0}'>{0}</a>", m.Value))
End If
Next
Return rawString
End Function
Screenshot