2013-11-21 32 views
0

我在aspx页面下面的代码:得到元素ID用vb

<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,在这种情况下,对象2。我想在vb.net的代码隐藏中做到这一点,但我不知道如何。任何帮助,将不胜感激。

回答

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 
+0

非常感谢您,先生。但是,objectSelect会根据选择哪个来更改,我如何确定选择了哪一个?像Me.objectSelect? – kevlar90

+0

更新回答提供你在找什么,对不起,我误解你的原意。 –