DataGrid的事件中,ItemCreated和ItemDataBound是尤为重要并且经常要用到的两个,其中的ItemCreated事件在DataGrid创建表格行时触发,而ItemDataBound则在数据绑定被绑定到相应的行后触发,前者可以用来在DataGrid中创建动态控件,比如下面的例子中在DataGrid的标题行中加入一个Button并注册它的Click事件,而ItemDataBound被触发时,数据已经绑定到相应的行,这时就可以获取当前行的数据,并根据这些数据来执行相应的操作,比如下面的示例中把绑定到“性别”字段的book值用string替换、为“禁止登录”列的CheckBox设置选中状态等,示例代码如下,保存为aspx文件后放在虚拟目录下即可运行:
<%@ Page Language="C#" Debug="True"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<script language="C#" runat="server">
void Page_Load(Object src, EventArgs e)
{
if(!Page.IsPostBack){
DataTable table=new DataTable();
table.Columns.Add(new DataColumn("编号",typeof(int)));
table.Columns.Add(new DataColumn("姓名",typeof(string)));
//性别列,用bool值表示,男为true,女为false
table.Columns.Add(new DataColumn("性别",typeof(bool)));
table.Columns.Add(new DataColumn("禁止登录",typeof(bool)));
DataRow row;
Random rd=new Random();
for(int i=0;i<10;i++){
row=table.NewRow();
row[0]=i+1;
row[1]="用户"+(i+1).ToString();
row[2]=(rd.Next(0,80)%2==0); //用随机数来生成性别,以示公正原则:)
row[3]=(i%2==0);
table.Rows.Add(row);
}
MyDataGrid.DataSource=table;
MyDataGrid.DataBind();
}
}
void MyDataGrid_ItemCreated(object src,DataGridItemEventArgs e){
if(e.Item.ItemType==ListItemType.Header){
Button btn=new Button();
btn.Text=e.Item.Cells[3].Text;
btn.Click+=new EventHandler(btn_Click);
e.Item.Cells[3].Controls.Clear();
e.Item.Cells[3].Controls.Add(btn);
}
}
void MyDataGrid_ItemDataBound(object src,DataGridItemEventArgs e){
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem){
DataRowView rv=(DataRowView)e.Item.DataItem;
//把性别列的bool值替换为相应的文字“男”或“女”
if((bool)rv["性别"]==true)
e.Item.Cells[2].Text="男";
else
e.Item.Cells[2].Text="女";
//根据“禁止登录”列的值设置CheckBox的选中状态
CheckBox ckb=e.Item.FindControl("ckb_denyLogin") as CheckBox;
if(ckb!=null){
ckb.Checked=(bool)rv["禁止登录"];
}
}
}
void btn_Click(object src,EventArgs e){
string deniedUsers=""; //保存用户选中的“禁止登录”的用户编号
CheckBox ckb;
for(int i=0;i<MyDataGrid.Items.Count;i++){
if(MyDataGrid.Items[i].ItemType==ListItemType.Item||MyDataGrid.Items[i].ItemType==ListItemType.AlternatingItem){
ckb=(CheckBox)MyDataGrid.Items[i].FindControl("ckb_denyLogin");
if(ckb!=null&&ckb.Checked){
deniedUsers+=MyDataGrid.DataKeys[i].ToString()+",";
}
}
}
deniedUsers=deniedUsers.Trim(',');
msglbl.Text="已被禁止登录的用户:"+deniedUsers;
}
</script>
<html>
<head>
<title> Webdiyer制造:)</title>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="Webdiyer(http://www.webdiyer.com)">
</head>
<body>
<form runat="server">
<ASP:DataGrid id="MyDataGrid" width="500px" runat="server" OnItemCreated="MyDataGrid_ItemCreated" OnItemDataBound="MyDataGrid_ItemDataBound" DataKeyField="编号" AutoGenerateColumns=false>
<HeaderStyle HorizontalAlign="center" BackColor="#9999CC"/>
<Columns>
<asp:BoundColumn DataField="编号" HeaderText="编号"/>
<asp:BoundColumn DataField="姓名" HeaderText="姓名"/>
<asp:BoundColumn DataField="性别" HeaderText="性别"/>
<asp:TemplateColumn HeaderText="禁止登录">
<ItemStyle HorizontalAlign="center"/>
<ItemTemplate>
<asp:CheckBox runat="server" id="ckb_denyLogin"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Label runat="server" id="msglbl"/>
</form>
</body>
</html>
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=225218