using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// MyTemplate 的摘要说明
/// </summary>
public class MyTemplate:ITemplate
{
#region ITemplate 成员
public void InstantiateIn(Control container)
{
Literal lbl = new Literal();
lbl.DataBinding += new EventHandler(lbl_DataBinding);//关键在这里
container.Controls.Add(lbl);
}
#endregion
private void lbl_DataBinding(object sender, EventArgs e)
{
Literal lbl = (Literal)sender;
GridViewRow objContainer = (GridViewRow)lbl.NamingContainer;
DataRowView row = objContainer.DataItem as DataRowView;
if (row != null)
{
lbl.Text = "<a href='" + row[0].ToString() + "'>" + row[0].ToString() + "</a>";
}
}
#region ITemplate 成员
#endregion
}
本文介绍如何在ASP.NET中实现自定义ITemplate接口来动态生成控件,并通过数据绑定将其内容设置为链接。

86

被折叠的 条评论
为什么被折叠?



