无法获取 ID 在 VB.Net 中的元素

Unable to Get Element with ID in VB.Net

我有下面的代码来在选中复选框时检索 table 元素。

有了这个,我可以获得 table ID。当我尝试获取带有 ID 的 HtmlTable 时,它​​抛出空引用异常。

获得 table 元素及其 ID 后,我需要遍历 table 的行。谁能帮帮我?

ASPX.VB

Protected Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs)
    Dim tab As String
    Dim check As CheckBox = CType(sender, CheckBox)
    tab = check.Parent.Parent.Parent.ID
    Dim tabb As HtmlControl = Me.FindControl(tab)
    Dim myTab As HtmlTable = CType(tabb, HtmlTable)
    For Each cell As HtmlTableRow In myTab.Rows
  //My Code Here
Next
End Sub

ASPX:

<table id="itemlist" class="mel-table" runat="server">
<tr>
<td>
    <asp:CheckBox runat="server" ID="CheckBox2" OnCheckedChanged="CheckBox2_CheckedChanged" AutoPostBack="true" />
</td>
</tr>
</table>

使用 table 嵌套在其下的父元素 ID,如下所示。

Protected Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs)
    Dim tab As String
    Dim check As CheckBox = CType(sender, CheckBox)
    tab = check.Parent.Parent.Parent.ID
    Dim myTab As HtmlTable = CType(mydiv.FindControl(tab), HtmlTable)
    For Each cell As HtmlTableRow In myTab.Rows
      //My Code Here
    Next
End Sub

<div id="mydiv" runat="server">
<table id="itemlist" class="mel-table" runat="server">
<tr>
<td>
    <asp:CheckBox runat="server" ID="CheckBox2" OnCheckedChanged="CheckBox2_CheckedChanged" AutoPostBack="true" />
</td>
</tr>
</table>
</div>