Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Label lblName =(Label)Master.Master.FindControl("bcr").FindControl("bcr").FindControl("Conditional1").FindControl("ctl03").FindControl("lblName");
- Public Module ControlExtensions
- <System.Runtime.CompilerServices.Extension()> _
- Public Function FindControlByID(ByRef SourceControl As Control, ByRef ControlID As String) As Control
- If Not String.IsNullOrEmpty(ControlID) Then
- Return FindControlHelper(Of Control)(SourceControl.Controls, ControlID)
- Else
- Return Nothing
- End If
- End Function
- Private Function FindControlHelper(Of GenericControlType)(ByVal ConCol As ControlCollection, ByRef ControlID As String) As Control
- Dim RetControl As Control
- For Each Con As Control In ConCol
- If ControlID IsNot Nothing Then
- If Con.ID = ControlID Then
- Return Con
- End If
- Else
- If TypeOf Con Is GenericControlType Then
- Return Con
- End If
- End If
- If Con.HasControls Then
- If ControlID IsNot Nothing Then
- RetControl = FindControlByID(Con, ControlID)
- Else
- RetControl = FindControlByType(Of GenericControlType)(Con)
- End If
- If RetControl IsNot Nothing Then
- Return RetControl
- End If
- End If
- Next
- Return Nothing
- End Function
- End Module
- /// <summary>
- /// Recursive FindControl method, to search a control and all child
- /// controls for a control with the specified ID.
- /// </summary>
- /// <returns>Control if found or null</returns>
- public static Control FindControlRecursive(Control root, string id)
- {
- if (id == string.Empty)
- return null;
- if (root.ID == id)
- return root;
- foreach (Control c in root.Controls)
- {
- Control t = FindControlRecursive(c, id);
- if (t != null)
- {
- return t;
- }
- }
- return null;
- }
- Label lblName = (Label) FindControlRecursive(Page, "lblName");
- }
- }
- public static class PageExtensionMethods
- {
- public static Control FindControlRecursive(this Control ctrl, string controlID)
- {
- if (string.Compare(ctrl.ID, controlID, true) == 0)
- {
- // We found the control!
- return ctrl;
- }
- else
- {
- // Recurse through ctrl's Controls collections
- foreach (Control child in ctrl.Controls)
- {
- Control lookFor = FindControlRecursive(child, controlID);
- if (lookFor != null)
- return lookFor;
- // We found the control
- }
- // If we reach here, control was not found
- return null;
- }
- }
Add Comment
Please, Sign In to add comment