mercredi 18 octobre 2017

FindControl.CheckBox inside a GridView

I have been given an ASP.Net app with VB CodeBehind to make some updates to. I'm not a VB coder, and am having difficulty getting the CodeBehind to see the CheckBox so I can capture its checked/unchecked state.

Here is the aspx of the CheckBoxes inside a Gridview:

<asp:TemplateField HeaderText="OT" SortExpression="Overtime">
<ItemTemplate>
    <asp:CheckBox ID="chkOTItem" runat="server" SortExpression="Overtime" Checked='<%#Eval("Overtime") %>' Enabled="false"></asp:CheckBox>
</ItemTemplate>
<EditItemTemplate>
    <asp:CheckBox ID="chkOT" runat="server" SortExpression="Overtime" DataField="Overtime"  OnCheckedChanged="chkOT_CheckChanged" AutoPostBack="true" EnableViewState="true" Checked='<%#Bind("Overtime") %>'></asp:CheckBox>
</EditItemTemplate>

<asp:TemplateField HeaderText="Double<br />OT" SortExpression="chkDblOTItem">
<ItemTemplate>
    <asp:CheckBox ID="chk2OTItem" runat="server" SortExpression="DoubleOT" Checked='<%#Eval("DoubleOT") %>' Enabled="false"></asp:CheckBox>
</ItemTemplate>
<EditItemTemplate>
    <asp:CheckBox ID="chk2OT" runat="server" SortExpression="DoubleOT" DataField="DoubleOT" OnCheckedChanged="chk2OT_CheckChanged" AutoPostBack="true" EnableViewState="true" Checked='<%#Bind("DoubleOT") %>'></asp:CheckBox>
</EditItemTemplate>

We have Overtime and Double Overtime Checkboxes. When one is checked, the other must be clear. We cannot allow checking of both at the same time.

In the CodeBehind, the OnCheckChanged event is absolutely firing for each checkbox, but I cannot find the CheckBox to identify its Checked/Unchecked state. I have found a half-dozen different suggestions on StackOverflow, but all of them give me "Nothing" which I am taking is the C# equivalent of NULL.

A couple of current examples of attempts to ID this control:

Dim chkOT As CheckBox = TryCast(row.Cells(19).FindControl("chkOT"), CheckBox)
Dim chk2OT As CheckBox = TryCast(row.Cells(20).FindControl("chk2OT"), CheckBox)

Dim myOTCheckBox As CheckBox = CType(Me.gridTimesheet.FindControl("chkOT"), CheckBox)
Dim myOT2CheckBox As CheckBox = CType(Me.gridTimesheet.FindControl("chk2OT"), CheckBox)

Is there something obvious I am missing? It shouldn't be this hard to ID the CheckBoxes and capture their state.




Aucun commentaire:

Enregistrer un commentaire