class LinkColumn : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
LinkButton link = new LinkButton();
link.ID = "link";
container.Controls.Add(link);
}
}
private void CreateWoLinkColumn(string headerText)
{
var column = new TemplateField() { HeaderText = headerText };
column.ItemTemplate=new LinkColumn();
column.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
column.HeaderStyle.Wrap = false;
MainDG.Columns.Add(column);
}
//Page_init叫用
protected void Page_Init(object sender, EventArgs e)
{
CreateWoLinkColumn("aa");
}
*動態生成Template 因每次Postback時,會重跑Page_Load function,故在第一次生成動態Template時,需寫在Page_Init 以保存控制項,爾後要取控制項的值才FindControl的到
*Page 生命週期:PreInit=>Init=>InitComplete=>PreLoad=>Load.....
參考資料:
http://blog.sina.com.cn/s/blog_8038620001012s9d.html