Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I developed a web-based training matrix that shows the training record for each employee in each division in my department in the company. The matrix will show many columns such as the employee name, username, job title... etc. what I want now is to make the username for each employee to be clickable which means when the admin clicks on it, the outlook will be opened with his email and the admin will be able to send him a message. In my company, this is possible because each employee email is mainly as: username@companyName.com so how to do that?

By the way, the username of the employee will be retrieved from the database using a storedprocedure. The user column is the 4th column in the GridView.

what I did is the following:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

	    if (e.Row.RowType == DataControlRowType.DataRow) 
	    {
            //Add mailto to the Username column
            e.Row.Cells[3].Text = "<a href='mailto:'" + e.Row.Cells[3].Text + "@companyName.com" + " />" + e.Row.Cells[3].Text + "</a>";
            }
     }


I could be able to let the username of each employee to be clickable, but I could not be able to put his email in the Outlook what it is opened, so how to do that?
Posted
Comments
zyck 12-Feb-12 3:38am    
can you post the markup code?
zyck 12-Feb-12 4:05am    
determine if the computer has outlook installed

1 solution

I prefer to do it with a method and a TemplateField.

In your markup:
ASP.NET
<asp:GridView id="GridView1" runat="server">
    <Columns>
        <!-- Other columns here -->
        <asp:TemplateField HeaderText="User Name">
            <ItemTemplate>
                <%# GetMailtoLink(Eval("UserName")) %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


And then in your code-behind:
C#
protected string GetMailtoLink(object userName)
{
    if (userName == null || String.IsNullOrEmpty(userName.ToString()))
        return null;
    return String.Format("<a href="\"mailto:{0}@companyName.com\"">{0}</a>", userName.ToString());
}


Mind you, this will open in the user's default email program as set in Windows. From a web site there is no way I know of to specify Outlook, but if you're developing this for internal company employees then you can probably make some valid assumptions as to what software they will have installed on their machine.

Also, we've sometimes had issues in the past due to security settings in Internet Explorer. If they are getting errors when they click the mailto link, check to make sure IE allows it in the security options for the zone (Intranet, or whatever it is your site is in).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900