【问题标题】:Unable to Get Element with ID in VB.Net无法在 VB.Net 中获取具有 ID 的元素
【发布时间】:2015-07-21 13:32:03
【问题描述】:

当复选框被选中时,我有下面的代码来检索表格元素。

有了这个,我可以得到表 ID。当我尝试获取带有 ID 的 HtmlTable 时,它​​会引发空引用异常。

获得带有 ID 的表格元素后,我需要遍历该表格的行。谁能帮帮我?

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>

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多