用vb获取元素id

时间:2013-11-21 16:29:13

标签: html asp.net vb.net

我在aspx页面中有以下代码:

<div id="objectList" style="overflow: auto; width:100px; display:block;      
position:absolute;top:0px;left:0px;z-index:100;">

<div id="object8" class="object" title="">
<br>object8</div>

<div id="object2" class="objectSelect" title="">
<br>object2</div>
</div>

我试图找到所选对象的ID,在本例中为object2。我试图在vb.net的代码隐藏中做到这一点,但我不知道如何。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

runat="server"添加到您想要查找的所有<div>元素中,如果选中它们,请执行以下操作:

<div id="object8" class="object" title="" runat="server">
<div id="object2" class="objectSelect" title="" runat="server">

现在在代码隐藏中,您可以遍历页面中的所有<div>元素并检查class属性值,如下所示:

For Each item As Control In Me.Controls
    ' We have to look at all HtmlGenericControl, because 
    ' there is no .NET control type for DIV 
    Dim theDiv As System.Web.UI.HtmlControls.HtmlGenericControl = TryCast(item, System.Web.UI.HtmlControls.HtmlGenericControl)

    ' Make sure the cast worked before we try to use the DIV
    If theDiv IsNot Nothing Then
        ' Is the class name equal to objectSelect?
        If theDiv.Attributes("class") = "objectSelect" Then
            ' Yes, this DIV is selected, do something here

        End If
    End If
Next