Recursive FindControl by Michael Palermo
Recursive FindControl by Michael Palermo
Need to find a control anywhere on the page or in a template? Here is the code to find it recursively. This example assumes the code is located in a custom base page.
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 | <span style="color: teal;"> 1</span> <span style="color: blue;">public</span> T FindControl<T>(<span style="color: blue;">string</span> id) where T : Control <span style="color: teal;"> 2</span> { <span style="color: teal;"> 3</span> <span style="color: blue;">return</span> FindControl<T>(Page, id); <span style="color: teal;"> 4</span> } <span style="color: teal;"> 5</span> <span style="color: teal;"> 6</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> T FindControl<T>(Control startingControl, <span style="color: blue;">string</span> id) where T : Control <span style="color: teal;"> 7</span> { <span style="color: teal;"> 8</span> <span style="color: green;">// this is null by default</span> <span style="color: teal;"> 9</span> T found = <span style="color: blue;">default</span>(T); <span style="color: teal;"> 10</span> <span style="color: teal;"> 11</span> <span style="color: blue;">int</span> controlCount = startingControl.Controls.Count; <span style="color: teal;"> 12</span> <span style="color: teal;"> 13</span> <span style="color: blue;">if</span> (controlCount > <span style="color: maroon;">0</span>) <span style="color: teal;"> 14</span> { <span style="color: teal;"> 15</span> <span style="color: blue;">for</span> (<span style="color: blue;">int</span> i = <span style="color: maroon;">0</span>; i < controlCount; i++) <span style="color: teal;"> 16</span> { <span style="color: teal;"> 17</span> Control activeControl = startingControl.Controls[i]; <span style="color: teal;"> 18</span> <span style="color: blue;">if</span> (activeControl <span style="color: blue;">is</span> T) <span style="color: teal;"> 19</span> { <span style="color: teal;"> 20</span> found = startingControl.Controls[i] <span style="color: blue;">as</span> T; <span style="color: teal;"> 21</span> <span style="color: blue;">if</span> (<span style="color: blue;">string</span>.Compare(id, found.ID, <span style="color: maroon;">true</span>) == <span style="color: maroon;">0</span>) <span style="color: blue;">break</span>; <span style="color: teal;"> 22</span> <span style="color: blue;">else</span> found = <span style="color: blue;">null</span>; <span style="color: teal;"> 23</span> } <span style="color: teal;"> 24</span> <span style="color: blue;">else</span> <span style="color: teal;"> 25</span> { <span style="color: teal;"> 26</span> found = FindControl<T>(activeControl, id); <span style="color: teal;"> 27</span> <span style="color: blue;">if</span> (found != <span style="color: blue;">null</span>) <span style="color: blue;">break</span>; <span style="color: teal;"> 28</span> } <span style="color: teal;"> 29</span> } <span style="color: teal;"> 30</span> } <span style="color: teal;"> 31</span> <span style="color: blue;">return</span> found; <span style="color: teal;"> 32</span> } |
You May Also Like
A Simple Introduction to Cisco CML2
0 3698 0Mark Jacob, Cisco Instructor, presents an introduction to Cisco Modeling Labs 2.0 or CML2.0, an upgrade to Cisco’s VIRL Personal Edition. Mark demonstrates Terminal Emulator access to console, as well as console access from within the CML2.0 product. Hello, I’m Mark Jacob, a Cisco Instructor and Network Instructor at Interface Technical Training. I’ve been using … Continue reading A Simple Introduction to Cisco CML2
How to use the PowerShell ConvertFrom- CSV Cmdlet to Save Coding Time with PS Script
0 322 4In this video, PowerShell instructor Jason Yoder shows how to use the ConvertFrom-CSV PowerShell Cmdlet to easily convert standard CSV files into PowerShell objects and speed up coding time. For instructor-led PowerShell training classes, see our course schedule: Microsoft Windows PowerShell Training PowerShell ConvertFrom-CSV script used in this video. Download ConvertFrom-CSV PowerShell Script <# ╔══════════════════════════════════════════════════════════════════════════════╗ … Continue reading How to use the PowerShell ConvertFrom- CSV Cmdlet to Save Coding Time with PS Script
How to use AWS CloudFormation templates to automate solutions inside Amazon AWS
0 380 0In the AWS Class here at Interface, we actually build fully automated solutions with AWS CloudFormations. Here’s an overview of using AWS templates using CloudFormation. CloudFormation is basically an “infrastructure-as-code tool” where you can go into a declarative document and define all the resources that you want and feed the document into the CloudFormation … Continue reading How to use AWS CloudFormation templates to automate solutions inside Amazon AWS