我正在爲我的學校的內部目錄服務工作,並且在代碼隱藏中在動態生成的gridview上創建「mailto:」鏈接時出現問題。在代碼隱藏中創建一個動態生成的gridview的mailto鏈接
經過一段時間的修補之後,我發現Hyperlinkfields不支持「:」字符 - 使得「mailto:」鏈接不可能。
因爲我動態生成Gridviews以允許按標題分組,所以我在代碼隱藏中完成了所有工作。 Gridviews是從數據源讀取創建的,並添加到aspx頁面上的佔位符標記以及定義該組的相應標籤。
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();
}
}
有沒有一種很好的方法來在代碼隱藏中創建一個mailto鏈接?由於需要動態創建,我無法將其轉換爲aspx文件。我在這裏錯過了什麼嗎?任何幫助或建議,不勝感激。謝謝。
編輯:鏈接需要顯示名稱而不是電子郵件地址,需要調用數據表中的兩列:[1]和[4]。
明白了,代碼全部在下面。
凡我gridview的東西是:
tfName.ItemTemplate = new NameColumn();
這是所需的TemplateField類。這是一個有點亂用文字標籤,但我對於工作的解決方案只是很高興:
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";
}
}
}
感謝您的建議,只是試了一下。沒有工作:/ –