如何在VB.net中的表中为每个img设置src

时间:2013-08-08 15:26:22

标签: html vb.net

如果我想循环遍历以下单元格的img src,请使用For Each循环并根据另一个值(x)为每个循环设置src,我可以明确地在代码中引用img src属性,或者是最好将表嵌入层次结构中的控件中。或者这可能会更好地使用javascript操作客户端?我已经编写了一个基本函数来检查x的值,如下所示。我在正确的路线上吗?

    Public Function GetCheckbox(ByVal x As Integer) As String
            Dim checkboxSrc As String = String.Empty
            Dim checked As String = "~/graphics/checkbox-checked.jpg"
            Dim unchecked As String = "~/graphics/checkbox-unchecked.jpg"

            If x = 1 Then
                checkboxSrc = checked
            ElseIf x = 0 Then
                checkboxSrc = unchecked
            End If
            Return checkboxSrc
        End Function


<table id="tblHolisticConcerns" runat="server" border="2" >               
                    <tr id="trPracticalConcerns2" runat="server">                              
                        <td style="width:20%; height:25px; vertical-align:middle"><img id="img2" alt="Holistic Checkbox" runat="server" src="" style="width:16px; height:16px" />&nbsp;Child Care<asp:Literal ID="litChildCare" runat="server"></asp:Literal></td>
                        <td style="width:20%; height:25px; vertical-align:middle"><img id="img3" alt="Holistic Checkbox" runat="server" src="" style="width:16px; height:16px" />&nbsp;Communication<asp:Literal ID="litCommunication" runat="server"></asp:Literal></td>
                        <td style="width:20%; height:25px; vertical-align:middle"><img id="img4" alt="Holistic Checkbox" runat="server" src="" style="width:16px; height:16px" />&nbsp;Household Tasks<asp:Literal ID="litHouseholdTasks" runat="server"></asp:Literal></td>
                        <td style="width:20%; height:25px; vertical-align:middle"><img id="img5" alt="Holistic Checkbox" runat="server" src="" style="width:16px; height:16px" />&nbsp;Housing<asp:Literal ID="litHousing" runat="server"></asp:Literal></td>
                        <td style="width:20%; height:25px; vertical-align:middle"><img id="img6" alt="Holistic Checkbox" runat="server" src="" style="width:16px; height:16px" />&nbsp;Insurance<asp:Literal ID="litInsurance" runat="server"></asp:Literal></td>
                    </tr>
    </table>

1 个答案:

答案 0 :(得分:0)

是的,根据您的标记,您可以循环遍历所有图像:

For Each oRow As HtmlTableRow In tblHolisticConcerns.Rows

  CType(oRow.Cells(0).Controls(0), HtmlImage).Src = GetCheckbox(Value)

Next