2013-05-21 30 views
0

我正在尋找一種方法,可以遍歷我的頁面上的所有控件,只挑選出某些類型。在這種情況下,我只查找html元素textarea。方法來搜索所有控件,只挑出某種類型

我有這個循環...

For Each control As HtmlTextArea In myDiv.Controls.Cast(Of HtmlTextArea)() 

    If TypeOf Control Is HtmlTextArea Then 
     ...do something... 
    End If 

Next 

但是,當它達到比HtmlTextArea說,這是無法施放其他控制它總是失敗說控制HtmlTextArea。

感謝

回答

2

您需要使用OfType,而不是Cast

For Each control As HtmlTextArea In myDiv.Controls.OfType(Of HtmlTextArea)() 

    'If is no longer needed, control will be HtmlTextArea 
    ...do something... 

Next 

MSDN爲OfType

1

您還可以添加一個委託到最後,如果要挑出控制有特定屬性。在這種情況下,它僅查看可見的HtmlTextAreas:

For Each control As HtmlTextArea In myDiv.Controls.OfType(Of HtmlTextArea)().Where(Function(textArea) textArea.Visible = True) 

     'Do stuff here 
     control.InnerHtml = "I am visible" 

Next control 
相關問題