ASP.Net - ListView - Add CheckBox To Control A Bit Field

Here is a list view that is bound to a object dataset, which has only one column “ACTIVE” and it’s a bit field.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<asp:ListView 
ID="lv"
runat="server"
DataKeyNames="ID"
DataSourceID="ods_AccountSite"
InsertItemPosition="LastItem"
ItemContainerID="layoutTemplate">
<layoutTemplate>
<span class="lv">
<table>
<thead>
<tr>
<td>Active&lt;/td>
</tr>
</thead>
<tbody>
<div ID="itemPlaceholder" runat="server" />
</tbody>
</table>
</span>
</layoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:ImageButton
Id="btnEdit"
runat="Server"
CommandName="Edit"
ImageUrl="~/Graphics/Edit.gif" />
</td>
<td>
<%#Eval("Active")%></td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
</td>
<td>
<asp:CheckBox
ID="checkbox_Active"
checked='&lt;%# iif(eval("Active"),"1","0") %>'
runat="server" />
</td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr>
<td>
<asp:CheckBox
ID="checkbox_Active"
checked='&lt;%# iif(eval("Active"),"1","0") %>'
runat="server" />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>

I had to update the ItemInserting event in the code behind

1
2
3
4
5
6
7
8
9
10
11
Protected Sub lv_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles lv.ItemInserting

Dim Active As String _
= CType(lv.InsertItem.FindControl("checkbox_Active"), CheckBox).Checked.ToString

If CBool(Active) Then
e.Values("Active") = 1
Else
e.Values("Active") = 0
End If
End Sub

Item Updating has a method of it’s own - Actually the ODS

1
2
3
4
5
6
7
8
9
10
11
12
13
Protected Sub ods_tblAssetType_StatusCode_Def_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles ods_tblAssetType_StatusCode_Def.Updating

e.InputParameters("AssetType") = lblAssetCode.Text.ToString

Dim checkbox_VerifiableStatus As String _
= CType(lv_tblAssetType_StatusCode_Def.EditItem.FindControl("checkbox_VerifiableStatus"), CheckBox).Checked.ToString

If CBool(checkbox_VerifiableStatus) Then
e.InputParameters("VerifiableStatus") = True
Else
e.InputParameters("VerifiableStatus") = False
End If
End Sub