Email Link for Name Field in Datagrid

  • Two fields are to be combined into one. ie: Email field and Name field should be displayed as Name field with Hyperlink to its email
  • To perform this task we need to create email field as invisible field.
  • And we need to convert Name field to template field which consist of two controls Label which is invisible and a hyperlink control.

private void DataGrid1_ItemDataBound(object sender,

System.Web.UI.WebControls.DataGridItemEventArgs e)

{

HyperLink hlk = new HyperLink();

Label lblName = new Label();

Label lblEmail = new Label();

if(e.Item.ItemType == ListItemType.Item ||

e.Item.ItemType == ListItemType.AlternatingItem)

{

hlk =(HyperLink) e.Item.Cells[1].FindControl("HyperLink1");

if(hlk != null)

{

lblName =(Label) e.Item.Cells[1].FindControl("lblName");

lblEmail =(Label)

e.Item.Cells[2].FindControl("lblEmail");

if(lblName != null )

{

hlk.Text = lblName.Text;

if(lblEmail != null)

hlk.NavigateUrl = "mailto:" + lblEmail.Text;

else

hlk.NavigateUrl = "mailto: noemail@email.com";

}

}

}

No comments:

Most Recent Post

Most Recent Ado.net FAQ

Most Recent .Net Framework FAQ

Most Recent Configuration Files FAQ

Daily Quote, Motivation, Inspiration and More

Subscribe Blog via Email

Enter your email address: