Click here to Skip to main content
15,880,796 members
Articles / Web Development / ASP.NET
Article

Automatically hyperlink URL and Email in ASP.NET Pages with C#

Rate me:
Please Sign up or sign in to vote.
3.57/5 (8 votes)
2 Apr 20061 min read 94.3K   1.3K   39   7
This is a article about how to hyperlink URL and Email in ASP.NET Pages with C#

Sample Image - Autohyperlink.jpg

Introduction

When I designed the web site http://www.outsourcexp.com/ , I found that many users post URL and Email information, but these URLs and Emails just display as text, if you want to visit these URLs or Emails ,you have to copy them then paste to your browser or outlook. I searched articles about Automatically hyperlink URLs but only found VB source code. so I decide do it myself.
The first step is how to detect URL and Email, of cause the best way is regular expressions. By using regular expressions, you can automatically detect hyperlink-sensitive text and automatically wrap them into element pointing to the same URL. Let's see how to proceed. The idea is preprocessing any displayable text using ad hoc regular expressions to identify portions of text that are email addresses or Web site URLs.
You create a RegEx object and initialize it with the regular expression. Next, you call IsMatch on the input string to see whether at least one match is found. Next, you call the method Replace. Replace takes the input as its first argument.

First, when you use regular expressions,you must include System.Text.RegularExpressions,

using System.Text.RegularExpressions;


The second step is how to detect a URL,

Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
                 RegexOptions.IgnoreCase|RegexOptions.Compiled);
Here is how to detect a Email,
Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
                   RegexOptions.IgnoreCase|RegexOptions.Compiled);

When you detected a URL or Email, you need to replace it with <a href=...></a> ,I put all these into a function,

private void Button1_Click(object sender, System.EventArgs e)
{
    string strContent = InputTextBox.Text;
    Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
                     RegexOptions.IgnoreCase| RegexOptions.Compiled);
    strContent = urlregex.Replace(strContent, 
                 "<a href=\"$1\" target=\"_blank\">$1</a>");
    Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
                       RegexOptions.IgnoreCase| RegexOptions.Compiled);
    strContent = emailregex.Replace(strContent, "<a href=mailto:$1>$1</a>");
    lbContent.Text += "<br>"+strContent;
}

Here your go... Build this control library, add reference to your project and enjoy validating.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
I am a programmer who work for Offshore Outsourcing Inc. as well as run my own website, Outsourcing Software Development , which is a good place for freelancing activities,this website help people outsource software development to India,China or other countries, find freelance programmers and web designers all over the world.

Started out doing Classic ASP, and moved on from there. My skillset now encompasses ASP, ASP.NET / C#, C++, VC++, MFC, SQL Server.

Comments and Discussions

 
Generalruleta en linea Pin
Filly12310-Nov-10 21:50
Filly12310-Nov-10 21:50 
GeneralPerfect Pin
huynhtronghoan4-Nov-09 0:35
huynhtronghoan4-Nov-09 0:35 
Generaldetetecting email in non-deliverable message Pin
msanjay7517-May-07 0:58
msanjay7517-May-07 0:58 
Thanks a lot - found this pretty useful - wanted to extract email from a non - deliverable mail receipt, and wrote the following code based on your article

string strSubject = mail.Subject.ToLower();

if (strSubject.ToLower().IndexOf("undeliver") >= 0)
{
int idx = m.Body.IndexOf("following recipient(s)");
if (idx > 0)
{
Regex eMailRegEx = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
RegexOptions.IgnoreCase | RegexOptions.Compiled);

Match eMailId = eMailRegEx.Match(m.Body, idx);

idx = m.Body.IndexOf("following recipient(s)");

if (eMailId != null && eMailId.Value != null)
{
Print(eMailId.Value);
}
}
}

http://msanjay.weblogs.us
GeneralGood start Pin
Somanova4203-Apr-06 3:26
Somanova4203-Apr-06 3:26 
GeneralRe: Good start Pin
Roland Luo3-Apr-06 15:18
Roland Luo3-Apr-06 15:18 
GeneralRe: Good start Pin
kescape29-Aug-06 14:17
kescape29-Aug-06 14:17 
GeneralRe: Good start [modified] Pin
pvoutov7-Apr-10 5:24
pvoutov7-Apr-10 5:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.