Automatically Hyperlink URLs and E-Mail Addresses in ASP.NET Pages with C#

Introduction

This article introduces how to hyperlink URLs and e-mail addresses in ASP.NET pages with C#, When you design a forum or other Web site, the code is very useful.

Background

After I designed the Web site http://www.outsourcexp.com, I found that many users post URL and e-mail information, but these URLs and e-mails just display as text. If you want to visit these URLs or e-mail addressess, you have to copy them and then paste them to your browser or e-mail client. I searched articles about automatically hyperlinking URLs, but only found VB source code. So, I decided do it myself.

Using the Code

The first step is how to detect URLs and e-mail addresses. Of course, the best way is with regular expressions. By using regular expressions, you can automatically detect hyperlink-sensitive text and automatically wrap it into elements pointing to the same URL. Here's how to proceed. The idea is to preprocess any displayable text using ad hoc regular expressions to identify portions of text that are e-mail 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 Replace method. 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 to detect a URL:

Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
   RegexOptions.IgnoreCase|RegexOptions.Compiled);

Here is how to detect an e-mail address:

Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
   RegexOptions.IgnoreCase|RegexOptions.Compiled);

When you detect a URL or e-mail address, 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 you go. Build this control library, add references to your project, and enjoy validating.



About the Author

Roland Luo

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.

Downloads

Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • This ESG study by Mark Peters evaluated a common industry-standard disk VTl deduplication system (with 15:1 reduction ratio) versus a tape library with LTO-5, drives with full nightly backups, over a five-year period.  The scenarios included replicated systems and offsite tape vaults.  In all circumstances, the TCO for VTL with deduplication ranged from about 2 to 4 times more expensive than the LTO-5 tape library TCO. The paper shares recent ESG research and lots more. 

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds