Guest User

Untitled

a guest
Jun 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. Label lblName =(Label)Master.Master.FindControl("bcr").FindControl("bcr").FindControl("Conditional1").FindControl("ctl03").FindControl("lblName");
  2.  
  3. Public Module ControlExtensions
  4. <System.Runtime.CompilerServices.Extension()> _
  5. Public Function FindControlByID(ByRef SourceControl As Control, ByRef ControlID As String) As Control
  6. If Not String.IsNullOrEmpty(ControlID) Then
  7. Return FindControlHelper(Of Control)(SourceControl.Controls, ControlID)
  8. Else
  9. Return Nothing
  10. End If
  11. End Function
  12.  
  13. Private Function FindControlHelper(Of GenericControlType)(ByVal ConCol As ControlCollection, ByRef ControlID As String) As Control
  14. Dim RetControl As Control
  15.  
  16. For Each Con As Control In ConCol
  17. If ControlID IsNot Nothing Then
  18. If Con.ID = ControlID Then
  19. Return Con
  20. End If
  21. Else
  22. If TypeOf Con Is GenericControlType Then
  23. Return Con
  24. End If
  25. End If
  26.  
  27. If Con.HasControls Then
  28. If ControlID IsNot Nothing Then
  29. RetControl = FindControlByID(Con, ControlID)
  30. Else
  31. RetControl = FindControlByType(Of GenericControlType)(Con)
  32. End If
  33.  
  34. If RetControl IsNot Nothing Then
  35. Return RetControl
  36. End If
  37. End If
  38. Next
  39.  
  40. Return Nothing
  41. End Function
  42.  
  43. End Module
  44.  
  45. /// <summary>
  46. /// Recursive FindControl method, to search a control and all child
  47. /// controls for a control with the specified ID.
  48. /// </summary>
  49. /// <returns>Control if found or null</returns>
  50. public static Control FindControlRecursive(Control root, string id)
  51. {
  52. if (id == string.Empty)
  53. return null;
  54.  
  55. if (root.ID == id)
  56. return root;
  57.  
  58. foreach (Control c in root.Controls)
  59. {
  60. Control t = FindControlRecursive(c, id);
  61. if (t != null)
  62. {
  63. return t;
  64. }
  65. }
  66. return null;
  67. }
  68.  
  69. Label lblName = (Label) FindControlRecursive(Page, "lblName");
  70.  
  71. }
  72.  
  73. }
  74.  
  75. public static class PageExtensionMethods
  76. {
  77. public static Control FindControlRecursive(this Control ctrl, string controlID)
  78. {
  79. if (string.Compare(ctrl.ID, controlID, true) == 0)
  80. {
  81. // We found the control!
  82. return ctrl;
  83. }
  84. else
  85. {
  86. // Recurse through ctrl's Controls collections
  87. foreach (Control child in ctrl.Controls)
  88. {
  89. Control lookFor = FindControlRecursive(child, controlID);
  90. if (lookFor != null)
  91. return lookFor;
  92. // We found the control
  93. }
  94. // If we reach here, control was not found
  95. return null;
  96. }
  97. }
Add Comment
Please, Sign In to add comment