【转】GridView动态添加模板列(并为其添加事件和自动绑定数据)
GridView动态添加模板列 这个case 经常会遇到,网上相关资料也不少,
但是关于如何动态为添加在模板列中的控件添加事件却没有。本文就是为解决这种情况而写。
代码如下:
GridViewTemplateDemo.aspx
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeFile=
"GridViewTemplateDemo.aspx.cs"
Inherits=
"GridView_GridViewTemplateDemo"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<head id=
"Head1"
runat=
"server"
>
<title>GridView动态添加模板列的例子</title>
</head>
<body>
<form id=
"form1"
runat=
"server"
>
<asp:GridView ID=
"GridView1"
runat=
"server"
AutoGenerateColumns=
"False"
>
</asp:GridView>
</form>
</body>
</html>
GridViewTemplateDemo.aspx.cs
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
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;
public
partial
class
GridView_GridViewTemplateDemo : System.Web.UI.Page
{
private
DataTable getDateTable()
{
DataTable dt =
new
DataTable();
dt.Columns.Add(
new
DataColumn(
"ID"
,
typeof
(Int32)));
dt.Columns.Add(
new
DataColumn(
"Name"
,
typeof
(String)));
DataRow dr = dt.NewRow();
dr[0] = 1;
dr[1] =
"01"
;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 2;
dr[1] =
"01011"
;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 3;
dr[1] =
"0120"
;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 4;
dr[1] =
"01001"
;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 5;
dr[1] =
"0105"
;
dt.Rows.Add(dr);
return
dt;
}
protected
override
void
OnInit(EventArgs e)
{
TemplateField customField =
new
TemplateField();
customField.ShowHeader =
true
;
customField.HeaderTemplate =
new
GridViewTemplate(DataControlRowType.Header,
"动态添加列"
);
GridViewTemplate gvt =
new
GridViewTemplate(DataControlRowType.DataRow,
"lbtn"
,
"Name"
);
gvt.eh +=
new
GridViewTemplate.EventHandler(lbtn_Click);
customField.ItemTemplate = gvt;
GridView1.Columns.Add(customField);
base
.OnInit(e);
}
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
GridView1.DataSource = getDateTable();
GridView1.DataBind();
}
}
public
void
lbtn_Click(
object
sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(),
"test"
,
"alert('ok');"
,
true
);
}
}
public
class
GridViewTemplate : ITemplate
{
public
delegate
void
EventHandler(
object
sender, EventArgs e);
public
event
EventHandler eh;
private
DataControlRowType templateType;
private
string
columnName;
private
string
controlID;
public
GridViewTemplate(DataControlRowType type,
string
colname)
{
templateType = type;
columnName = colname;
}
public
GridViewTemplate(DataControlRowType type,
string
controlID,
string
colname)
{
templateType = type;
this
.controlID = controlID;
columnName = colname;
}
public
void
InstantiateIn(System.Web.UI.Control container)
{
switch
(templateType)
{
case
DataControlRowType.Header:
Literal lc =
new
Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break
;
case
DataControlRowType.DataRow:
LinkButton lbtn =
new
LinkButton();
lbtn.ID =
this
.controlID;
if
(eh !=
null
)
{
lbtn.Click +=
new
System.EventHandler(eh);
}
lbtn.DataBinding+=
new
System.EventHandler(lbtn_DataBinding);
container.Controls.Add(lbtn);
break
;
default
:
break
;
}
}
void
lbtn_DataBinding(
object
sender, EventArgs e)
{
LinkButton lbtn = sender
as
LinkButton;
if
(lbtn !=
null
)
{
GridViewRow container = lbtn.NamingContainer
as
GridViewRow;
if
(container !=
null
)
{
object
dataValue = DataBinder.Eval(container.DataItem, columnName);
if
(dataValue != DBNull.Value)
{
lbtn.Text = dataValue.ToString();
}
}
}
}
}