本文介绍如何在 ASP.NET 应用中处理 DataGrid 的 ItemDataBound 事件,通过不同条件判断对列表项和编辑项进行操作。针对列表项进行通用处理,并针对编辑状态下的特定控件 CheckBoxList 进行个性化设置。
ProtectedSub dgLimit_ItemDataBound()Sub dgLimit_ItemDataBound(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgLimit.ItemDataBound If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then dosomething() EndIf If e.Item.ItemType = ListItemType.EditItem Then'判断是否为编辑状态 Dim cbl As CheckBoxList =CType(e.Item.FindControl("cblEdit"), CheckBoxList) '例如EditItemTemplate有一cblEdit的控件 If (cbl IsNotNothing) Then'如果控件不为空 dosomethingwith(CType(e.Item.FindControl("cblEdit"), CheckBoxList)) '操作该控件 EndIf EndIf End Sub