2009-04-23 2 views
1

Я пытаюсь использовать коллекцию управления страницей с помощью LINQ. В то время как это работает:ASP.NET: Получение всех элементов управления страницы как IEnumerable

dim l = Me.Controls.OfType(Of TextBox).AsQueryable() 

следующее вернуть ArgumentExceptionError:

dim l = Me.Controls.AsQueryable() 

Мне нужно все элементы управления. Любая помощь? Благодаря

ответ

1

Вы пробовали:

Me.Controls.Cast(Of Control) 

Из интереса, зачем вам это как IQueryable? Вам не хватает IEnumerable<T>? (Это результат Cast.)

Проблема с просто вызовом AsQueryable заключается в том, что контрольная коллекция не реализует IEnumerable<T>, всего IEnumerable.

1

Кроме того, не забывайте, что элементы управления может быть вложенными, и просто просят страницу для его управления будет только сказать вам о прямых детях, но он не скажет вам о контроле в этих элементах управления:

Locate the web forms controls on a page by walking the Controls Collection

This example finds only the controls contained in the Page object and the controls that are direct children of the page. It does not find text boxes that are children of a control that is in turn a child of the page. For example, if you added a Panel control to page, the Panel control would be a child of the HtmlForm control contained by the Page, and it would be found in this example. However, if you then added a TextBox control into the Panel control, the TextBox control text would not be displayed by the example, because it is not a child of the page or of a control that is a child of the page. A more practical application of walking the controls this way would be to create a recursive method that can be called to walk the Controls collection of each control as it is encountered. However, for clarity, the example below is not created as a recursive function.

Смежные вопросы