Objective: This article describes how to do display clickable email links (mailto:) in a ASP.NET GridView.
Background: Often in our projects, it is a requirement to display clickable email links in a GridView perhapse along other information. Because of the initial and seemingly convoluted nature of ASP.NET, even a simple task as such can seem daunting. Well, fear not because this is really simple to do.
Procedure: Follow instructions below to accomplish this task. This article uses Visual Web Developer Express 2008, SQL Server Express 2005.
- Create a new web form in your site and name it anything.
- Drag and drop a GridView onto the page.
- Configure your DataSource. Select a table that displays email addresses.
- Once you have configured your GridView and DataSource, click on the GridView’s smart tag and select “edit columns”.
- In the selected fields window click to highlight the field that contains the email addresses.
- On the right side, click on the “convert this field into a TemplateField link.
- Click OK to close this window and go into source view look at GridView’s ItemTemplate. It should look something like this:
- Now let’s modify this code to create clickable mailto: email addresses. To do this, replace the above code with the one below.
This will result looking something like the image below:
- If you don’t want to show the actual email addresses as the link maybe to have all the links a uniform look and size, you can do something like the following.
This will result in something like the image below.
- That’s it! Enjoy.