Click here to Skip to main content
Click here to Skip to main content

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

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

Share

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.

Comments and Discussions

 
Generalruleta en linea PinmemberFilly12310-Nov-10 22:50 
GeneralPerfect Pinmemberhuynhtronghoan4-Nov-09 1:35 
Generaldetetecting email in non-deliverable message Pinmembermsanjay7517-May-07 1:58 
GeneralGood start PinmemberSomanova4203-Apr-06 4:26 
GeneralRe: Good start PinmemberRoland Luo3-Apr-06 16:18 
GeneralRe: Good start Pinmemberkescape29-Aug-06 15:17 
GeneralRe: Good start [modified] Pinmemberpvoutov7-Apr-10 6:24 

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

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

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