My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Ra-Ajax - A Managed Ajax Library for ASP.NET and Mono
* Copyright 2008 - 2009 - Thomas Hansen thomas@ra-ajax.org
* This code is licensed under the GPL version 3 which
* can be found in the license.txt file on disc.
*
*/

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace Ra.Selector
{
/**
* Helper class for doing selector queries on Controls to retrieve specific
* Controls in Page hierarchy. Kind of like a CSS selector only for the server-side.
* This class can be very handsome in scenarios where you have GridViews or Data Repeators
* which are dynamically databound and you for some reason cannot use the ID directly or something
* but still need to traverse server-side Control hierarchy to find a specific control...
*/
public static class Selector
{
/**
* Recursively searches Control hierarchy for matches for Predicate and returns it as T
*/
public static T SelectFirst<T>(Control from, Predicate<Control> predicate) where T : Control
{
if (predicate(from))
return from as T;
foreach (Control idx in from.Controls)
{
T tmpRetVal = SelectFirst<T>(idx, predicate);
if (tmpRetVal != null)
return tmpRetVal;
}
return null;
}

/**
* Recursively searches Control hierarchy for the first control that
* matches the type of T and returns it as T
*/
public static T SelectFirst<T>(Control from) where T : Control
{
return SelectFirst<T>(from,
delegate(Control idx)
{
return idx is T;
});
}

/**
* Recursively searches Control hierarchy for the first control that
* have the given ID and returns it as T. Kind of like the "recursive FindControl"
*/
public static T FindControl<T>(Control from, string id) where T : Control
{
return SelectFirst<T>(from,
delegate(Control idx)
{
return idx.ID == id;
});
}

/**
* Recursively search Control hierarcy for ALL controls that
* matches the given Predicate and returns them as T
*/
public static IEnumerable<T> Select<T>(Control from, Predicate<Control> predicate) where T : Control
{
if (from is T)
{
if (predicate(from))
yield return from as T;
}
foreach (Control idx in from.Controls)
{
foreach (T idxInner in Select<T>(idx, predicate))
{
yield return idxInner;
}
}
}

/**
* Recursively search Control hierarcy for ALL controls that
* matches the type of T and returns them as T
*/
public static IEnumerable<T> Select<T>(Control from) where T : Control
{
return Select<T>(from,
delegate(Control idx)
{
return idx is T;
});
}
}
}

Change log

r1540 by polterguy on Jul 14, 2009   Diff
Carriage return fixing...
Go to: 
Project members, sign in to write a code review

Older revisions

r1532 by kariem.elkoush on Jun 24, 2009   Diff
[No log message]
r1527 by kariem.elkoush on Jun 22, 2009   Diff
[No log message]
r1525 by kariem.elkoush on Jun 19, 2009   Diff
[No log message]
All revisions of this file

File info

Size: 3434 bytes, 100 lines
Powered by Google Project Hosting