How to set ASP.NET controls adapter by code ?

In a recently job I wanted to package as much as possible common items of the web layer (WebControls, BasePage, HttpHandlers, Js, Css, etc...) in a class library project, so I can share the dll across several ASP.NET apps.

As I use the CSS Friendly Control Adapters to change the asp:menu rendering, I could easily factorize adapters class, css, pictures and Js as embedded resources in the external dll, but I end up with the browser file in App_browsers folder (that maps webcontrols to their adapters).

Good news is you can set adapters by code in the PreInit event of your BasePage, sample for the Menu :

C#:

HttpContext.Current.Request.Browser.Adapters
[typeof(System.Web.UI.WebControls.Menu).AssemblyQualifiedName] =
typeof(YourNameSpace.MenuAdapter).FullName;

VB:

HttpContext.Current.Request.Browser.Adapters
(GetType(System.Web.UI.WebControls.Menu).AssemblyQualifiedName) =
    GetType(YourNameSpace.MenuAdapter).FullName()

No Comments