I am working on an internal directory service for my school and am having a problem creating "mailto:" links on a dynamically generated gridview from the codebehind.
After tinkering around for a while with this, I found out that Hyperlinkfields do not support the ":" character- making "mailto:" links not possible.
Because I am dynamically generating the Gridviews to allow for grouping by header, I am doing it all in the codebehind. The Gridviews are created reading from a datasource and added to a placeholder tag on the aspx page along with a corresponding label defining the group.
public void GenerateDynamicGVs()
{
//...
DataTable dt = new DataTable();
//...
using (SqlDataReader dr = DBUtility.ExecuteReader(cmd, "www_ConMasterDBString"))
{
dt.Load(dr);
var query = dt.AsEnumerable()
.GroupBy(r => r.Field<string>("GroupName"))
.Select(grp => new
{
GroupName = grp.Key,
})
.OrderBy(o => o.GroupName)
.ToList();
foreach (var item in query)
{
Label NewLabel = new Label();
NewLabel.Text = item.GroupName;
NewLabel.CssClass = "DirectoryHeaders";
GridView newGV = new GridView();
newGV.CssClass = "CONServices";
newGV.ShowHeader = false;
newGV.AutoGenerateColumns = false;
newGV.DataSource = dt.AsEnumerable().Where(p => p.Field<string>("GroupName") == item.GroupName)
.Select(p => new
{
id = p["id"].ToString(),
EntryName = p["EntryName"].ToString(),
EntryNumber = p["EntryNumber"].ToString(),
EntryContact = p["EntryContact"].ToString(),
Email = p["Email"].ToString(),
GroupName = p["GroupName"].ToString()
});
HyperLinkField hlName = new HyperLinkField();
BoundField bfNumber = new BoundField();
BoundField bfContact = new BoundField();
hlName.DataNavigateUrlFields = new string[] { dt.Columns[4].ToString() };
hlName.DataTextField = dt.Columns[1].ToString();
hlName.DataNavigateUrlFormatString = "mailto:{0}"; //This line executes, but is not a clickable link because of the colon.
bfNumber.DataField = dt.Columns[2].ToString();
bfContact.DataField = dt.Columns[3].ToString();
newGV.Columns.Add(hlName);
newGV.Columns.Add(bfNumber);
newGV.Columns.Add(bfContact);
divPlaceHolder.Controls.Add(NewLabel);
divPlaceHolder.Controls.Add(newGV);
newGV.DataBind();
}
dr.Close();
}
}
Is there a good way to create a mailto link in the codebehind? I can't convert it to the aspx file because of the need for dynamic creation. Am I missing something here? Any help or advice is greatly appreciated. Thanks.
EDIT: The link needs to have the name displayed rather than the email address, requiring calling two columns from the datatable: [1] and [4].
Got it working, code is all below.
Where my gridview stuff is:
tfName.ItemTemplate = new NameColumn();
The class that was required for the TemplateField. It is a bit messy with the literal tags, but I am just happy for a solution that worked:
class NameColumn : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lit1 = new Literal();
Literal litName = new Literal();
Literal lit3 = new Literal();
Literal litEmail = new Literal();
Literal lit5 = new Literal();
lit1.Text = "<a href=\"mailto:";
lit3.Text = "\">";
lit5.Text = "</a>";
litEmail.DataBinding += new EventHandler(LabelEmailDatabinding);
litName.DataBinding += new EventHandler(LabelNameDatabinding);
container.Controls.Add(lit1);
container.Controls.Add(litEmail);
container.Controls.Add(lit3);
container.Controls.Add(litName);
container.Controls.Add(lit5);
}
private void LabelNameDatabinding(object sender, EventArgs e)
{
Literal lit = (Literal)sender;
GridViewRow row = (GridViewRow)lit.NamingContainer;
lit.Text = DataBinder.Eval(row.DataItem, "EntryName").ToString();
}
private void LabelEmailDatabinding(object sender, EventArgs e)
{
Literal lit = (Literal)sender;
GridViewRow row = (GridViewRow)lit.NamingContainer;
lit.Text = DataBinder.Eval(row.DataItem, "Email").ToString();
if (lit.Text == null)
{
lit.Text = "test";
}
}
}