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

时间:2015-05-11 06:36:41

标签: asp.net vb.net html-table

当选中复选框时,我有以下代码来检索表元素。

有了这个,我可以得到表ID。当我尝试使用ID获取HtmlTable时,它会抛出空引用异常。

一旦我获得了带有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>

1 个答案:

答案 0 :(得分:0)

使用表格嵌套的父元素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>