2013-11-21 49 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

更新回答提供你在找什麼,對不起,我誤解你的原意。 –