Click here to Skip to main content
13,346,708 members (66,249 online)
Click here to Skip to main content
Add your own
alternative version


39 bookmarked
Posted 2 Apr 2006

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

, 2 Apr 2006
Rate this:
Please Sign up or sign in to vote.
This is a article about how to hyperlink URL and Email in ASP.NET Pages with C#

Sample Image - Autohyperlink.jpg


When I designed the web site , 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*)",
Here is how to detect a Email,
Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",

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.


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


About the Author

Roland Luo
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.

You may also be interested in...

Comments and Discussions

Generalruleta en linea Pin
Filly12310-Nov-10 22:50
memberFilly12310-Nov-10 22:50 
GeneralPerfect Pin
huynhtronghoan4-Nov-09 1:35
memberhuynhtronghoan4-Nov-09 1:35 
Follow your post, I solved my problem, thanks.


Generaldetetecting email in non-deliverable message Pin
msanjay7517-May-07 1:58
membermsanjay7517-May-07 1:58 
GeneralGood start Pin
Somanova4203-Apr-06 4:26
memberSomanova4203-Apr-06 4:26 
GeneralRe: Good start Pin
Roland Luo3-Apr-06 16:18
memberRoland Luo3-Apr-06 16:18 
GeneralRe: Good start Pin
kescape29-Aug-06 15:17
memberkescape29-Aug-06 15:17 
GeneralRe: Good start [modified] Pin
pvoutov7-Apr-10 6:24
memberpvoutov7-Apr-10 6: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 3 Apr 2006
Article Copyright 2006 by Roland Luo
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid