Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp Auto Convert Text To URL Link And MailTo Email Address With ASP.NET C#

Auto Convert Text To URL Link And MailTo Email Address With ASP.NET C#

The ASP.NET C# code below will automatically detect if your string content contains url[s] or email address[es] then wrap them into hyperlink[s] or mailto email address[es] respectively.

The example below makes a URL and an email address click-able. If you click on the URL, it will open the URL in a new web page and if you click on the email address, it will open a default email composer which is config in your current browser.

The ASP.NET C# source code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
 
public partial class convert_text_to_url_and_mailto : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string content = "Welcome to http://4rapiddev.com" + ", a website for developers, designers and webmaster.<br>";
        content += "If you have any feedback, question or contribution please send an email to [email protected]";
 
        Regex url_regex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
 
        Regex email_regex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
 
        content = url_regex.Replace(content, "<a href=\"$1\" target=\"_blank\">$1</a>");
        content = email_regex.Replace(content, "<a href=mailto:$1>$1</a>");
 
        Response.Write(content);
    }
}

using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text.RegularExpressions; public partial class convert_text_to_url_and_mailto : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string content = "Welcome to http://4rapiddev.com" + ", a website for developers, designers and webmaster.<br>"; content += "If you have any feedback, question or contribution please send an email to [email protected]"; Regex url_regex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", RegexOptions.IgnoreCase | RegexOptions.Compiled); Regex email_regex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", RegexOptions.IgnoreCase | RegexOptions.Compiled); content = url_regex.Replace(content, "<a href=\"$1\" target=\"_blank\">$1</a>"); content = email_regex.Replace(content, "<a href=mailto:$1>$1</a>"); Response.Write(content); } }

The HTML Output

Welcome to <a href="http://4rapiddev.com," target="_blank">http://4rapiddev.com,</a> a website for developers, designers and webmaster.<br>If you have any feedback, question or contribution please send an email to <a href=mailto:[email protected]>[email protected]</a>

Welcome to <a href="http://4rapiddev.com," target="_blank">http://4rapiddev.com,</a> a website for developers, designers and webmaster.<br>If you have any feedback, question or contribution please send an email to <a href=mailto:[email protected]>[email protected]</a>

Note: it’s required to use the Namespace: System.Text.RegularExpressions

May 16, 2011Hoan Huynh
Manage Or Organize WordPress Media Library Structure In CategoriesUpgrade/Switch To PHP 5.0 5.1 5.2 5.3 With .htaccess
You Might Also Like:
  • CSharp Validate Email Address Function With Regular Expression
  • Validate Email Address Format Using PHP Regular Expression preg_match
  • Simple PHP Code Send Email
  • Setup Email Forwarding In GMail
  • Feedburner Enable Email Subscription
  • Send Email Via Gmail SMTP Authentication With PHPMailer
  • ASP.NET JQuery Autocomplete Textbox
  • Auto Rotate Web Page Title With JavaScript
  • Change Email Signature In Mozilla Thunderbird
  • SQL SERVER How to import Text/CSV file into table
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago CSharpmailto, Regex, RegularExpressions818
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,237 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,087 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,872 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,343 views
C# Read Json From URL And Parse/Deserialize Json
9,851 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Term Papers – Easy to Take Care of
  • How to Write Term Papers in Online Tutorials
  • How to Plan Writing an Essay
  • How to Apply For a Payday Loan With Bad Credit
  • Statistics For Sale – How To Compose One
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (123)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development