Automatically add hyperlinks to URL addresses in ASP. NET

Source: Internet
Author: User

As a programmer, after completing the design, he must constantly improve the program based on the Program situation and user feedback so as to continuously improve his work. After I made a software business network http://www.bizsofts.com forum, I found that people always like to add a variety of useful URL links or e-mail addresses in the post. I didn't take this into consideration when I designed it, so that these URL links or Email addresses can only be displayed in the form of text instead of hyperlinks, other users who browse the post must copy these URL links to the browser or copy the Email address to Outlook to transfer the link address or send an Email to the corresponding Email address.
After finding this problem, I will start to solve it. First, I searched for the current code on the Internet. Unfortunately, I did not find any articles in this regard on the search engine. Later, I thought about writing one by myself using ASP. NET.
The key to automatically display hyperlinks is how to correctly identify hyperlinks. Undoubtedly, the most effective method is to use regular expressions. A regular expression is a text pattern consisting of common characters (such as characters a to z) and special characters (such as metacharacters). It describes a string matching pattern, it can be used to check whether a string contains a seed string, replace matched substrings, or retrieve substrings that meet certain conditions from a string .. NET base class library contains a namespace and a series of classes that can fully utilize the power of Rule expressions. With this class, you can automatically detect URL links or Email addresses in text. Next I will explain in detail how to use ASP. NET (C #) to achieve our goal step by step:

First, to use a regular expression in ASP. NET (C #), you must include the System. Text. RegularExpressions namespace:

Using System. Text. RegularExpressions;

Step 2: use regular expressions to identify URL hyperlinks:

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

The code here is to use a regular expression to identify the Email address:

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

Step 3: After the Program recognizes a URL hyperlink or Email address, you must use <a href =...> hyperlinks </a> replace these hyperlinks to display the text as links. Here I will include all of them in the function:

Private void button#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 = \" \ "target = \" _ blank \ "> </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:> </a> ");
LbContent. Text + = "<br>" + strContent;
}

After completing the preceding steps, you can automatically display hyperlinks and Email addresses on the webpage. Welcome to download the source code of this example, as well as to the http://www.bizsofts.com of the Forum to watch the actual effect. (Note: The English version of this article has been published on CodePoject and CodeGuru)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.