Expanding opportunities of Classic ASP.NET with generic extension function FindBaseClassRecursively.
Classic ASP.NET application often has not simple structure. Common template is - there are Master page with including many controls, many pages with repiters and many levels including controls. But ASP.NET structure building by the way when function only in APP_CODE is accesible to all application, pages is placed in another folder and page function is not accessible from all application.
So, all common function of ASP.NET application moving to APP_CODE folder, and very often page has many level of inheritance hierarchy. But how in control find needed class and simple cast it to fixed class? Especially, if project in active development and interface appears and disappears each days, new level of inheritance page is appears and disappears each days. I don't want to change page's control code each days, even if control in master page moving from one level to another. I want have stable page's control code and want have stability found method of master page's control.
In the screen below we may see common template of ASP.NET pages, this is very typical application.
For example in basket control placed in master page I have some needed interface to me, what simplest way to found it in control, that repeated in page? How to find method of basket, when button "Add to basket" will be pressed in first screen, of button "Cancel" in second screen?
And additionally, I need to know in control tabs number of paged, that consist repeater of control (second screen has three tabs). For this many purposes, I had write generic extension function for ASP.NET page:
1: Imports Microsoft.VisualBasic
2:
3: Public Module Extension2
4:
5: <Runtime.CompilerServices.Extension()> _
6: Public Function FindBaseClassRecursively(Of T As Class)(ByVal CurrentControl As Control) As T
7:
8: If TryCast(CurrentControl, T) IsNot Nothing Then
9: Return TryCast(CurrentControl, T)
10: Exit Function
11: Else
12: If CurrentControl.HasControls Then
13: For Each c As Control In CurrentControl.Controls
14: FindBaseClassRecursively(Of T)(c)
15: Next
16: End If
17: End If
18:
19: End Function
20: End Module
With this function my code is simplified and come independence of adding level of inheritance of page and moving control from with needed function from one place of master page to another.