Change a CSS Control Adapter in Code
November 11th, 2008 by Remy Blaettler
I run into a situation where I needed different CSS Control Adapters for the ASP.NET Menu Control for differnent browsers, in my case a default one and one for the iPhone.
So my first idea was to just put multiple controlAdapters sections into a .browser file, but for whatever reason that did not work (I still think this should work somehow, if you have the answer, let me know). Then I was looking to just change the adapter in my code, but even Scott Guthrie said it’s not possible. Ok, time to call it a day and get a beer (or two).
But who cares if the Corporate Vice President in the Microsoft Developer Division says it’s not possible, lets go at it again. Eventually I found this post. If it is possible to add and remove Adapters at run time, it clearly has to be possible to change them, no? And indeed, it is possible:
protected override void OnPreInit(EventArgs e) { HttpContext.Current.Request.Browser.Adapters["System.Web.UI.WebControls.Menu, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"] = "Supertext.Adapters.iUiMenuAdapter"; base.OnPreInit(e); } protected override void OnUnload(EventArgs e) { base.OnUnload(e); HttpContext.Current.Request.Browser.Adapters["System.Web.UI.WebControls.Menu, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"] = "CSSFriendly.MenuAdapter"; }
Just add the above code to your aspx page and change the key/value pair appropriately. You can iterate through the Adapters list to get a better idea what is in there and how to replace it.
Now, this is not exactly a schoolbook code sample, but it works and it’s just intended to give you kick in the right direction. In that sense, please no of these “this is unmaintainable code” comments. Thanks!
