我们要实现一种效果,比如datagird中一行有多个checkbox,当我们想选择某一个checkbox,而其它也被选择,我们可以这样做
.首先添加3个模版列,分别为Ch1,Ch2,Ch3,如图
然后在datagrid的itemCreated事件中绑定程序
最后得到的界面如图所示:

.首先添加3个模版列,分别为Ch1,Ch2,Ch3,如图
1
<asp:TemplateColumn>
2
<ItemTemplate>
3
<asp:CheckBox ID="Ch1" Runat="server"></asp:CheckBox>
4
</ItemTemplate>
5
</asp:TemplateColumn>
6
<asp:TemplateColumn>
7
<ItemTemplate>
8
<asp:CheckBox ID="Ch2" Runat="server"></asp:CheckBox>
9
</ItemTemplate>
10
</asp:TemplateColumn>
11
<asp:TemplateColumn>
12
<ItemTemplate>
13
<asp:CheckBox ID="Ch3" Runat="server"></asp:CheckBox>
14
</ItemTemplate>
15
</asp:TemplateColumn>
<asp:TemplateColumn>2
<ItemTemplate>3
<asp:CheckBox ID="Ch1" Runat="server"></asp:CheckBox>4
</ItemTemplate>5
</asp:TemplateColumn>6
<asp:TemplateColumn>7
<ItemTemplate>8
<asp:CheckBox ID="Ch2" Runat="server"></asp:CheckBox>9
</ItemTemplate>10
</asp:TemplateColumn>11
<asp:TemplateColumn>12
<ItemTemplate>13
<asp:CheckBox ID="Ch3" Runat="server"></asp:CheckBox>14
</ItemTemplate>15
</asp:TemplateColumn>然后在datagrid的itemCreated事件中绑定程序
1
Private Sub dgPeople_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgPeople.ItemCreated
2
Select Case e.Item.ItemType
3
Case ListItemType.Item, ListItemType.AlternatingItem
4
5
6
Dim chkAllOK As CheckBox = CType(e.Item.FindControl("Ch3"), CheckBox)
7
chkAllOK.AutoPostBack = True
8
9
AddHandler chkAllOK.CheckedChanged, AddressOf ChkChanged //绑定程序
10
11
End Select
12
End Sub
下面为chkChanged绑定程序
Private Sub dgPeople_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgPeople.ItemCreated2
Select Case e.Item.ItemType3
Case ListItemType.Item, ListItemType.AlternatingItem4
5

6
Dim chkAllOK As CheckBox = CType(e.Item.FindControl("Ch3"), CheckBox)7
chkAllOK.AutoPostBack = True8
9
AddHandler chkAllOK.CheckedChanged, AddressOf ChkChanged //绑定程序10

11
End Select12
End Sub 1
Private Sub ChkChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
2
Dim chkAllOK As CheckBox = CType(sender, CheckBox)
3
Dim unitID = chkAllOK.UniqueID //获得控件的unitID方便下面在datagrid中查找item
4
Dim item As DataGridItem
5
Dim curItem As DataGridItem
6
If chkAllOK.Checked Then
7
8
Dim obj As Control
9
10
For Each item In Me.dgPeople.Items
11
obj = item.FindControl("Ch3")
12
If unitID = obj.UniqueID Then
13
curItem = item
14
Exit For
15
End If
16
Next
17
18
Dim ch1 As CheckBox = CType(curItem.FindControl("Ch1"), CheckBox)
19
ch1.Checked = True
20
Dim ch2 As CheckBox = CType(curItem.FindControl("Ch2"), CheckBox)
21
ch2.Checked = True
22
End If
23
24
End Sub
:
Private Sub ChkChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)2
Dim chkAllOK As CheckBox = CType(sender, CheckBox)3
Dim unitID = chkAllOK.UniqueID //获得控件的unitID方便下面在datagrid中查找item4
Dim item As DataGridItem5
Dim curItem As DataGridItem6
If chkAllOK.Checked Then7
8
Dim obj As Control9

10
For Each item In Me.dgPeople.Items11
obj = item.FindControl("Ch3")12
If unitID = obj.UniqueID Then13
curItem = item14
Exit For15
End If16
Next17

18
Dim ch1 As CheckBox = CType(curItem.FindControl("Ch1"), CheckBox)19
ch1.Checked = True20
Dim ch2 As CheckBox = CType(curItem.FindControl("Ch2"), CheckBox)21
ch2.Checked = True22
End If23

24
End Sub最后得到的界面如图所示:


浙公网安备 33010602011771号