Thursday, May 12, 2005

Dot Net (.Net)? Not Yet

UPDATE: I've posted the VB.Net code sample in another post.

Off-late I've learning quite a few things about .net (mostly asp.net) the hard way (actually that is true as of now for almost everything in life). Anywez here are few more things that I found out.....
One of the requirement in this project is to show a list of RadioButtons within a DataGrid (could be a DataList or Repeater for that matter). Now the user gets to see the list of conferences in a grid and he can choose one (only one) from that list. So basically all I had to do was define a templatecolumn in the grid, include a RadioButton within the template and give it a groupname (the groupname ensures that the radiobuttons rendered are mutually exclusive). Seems to be a 5 mins job but guess what? I did exactly the same and when I ran the code, I could select all of the radio buttons within the grid at the same time. I checked the source html and found out that all the radio buttons rendered had unique names, thought I did something wrong so I debugged and debugged but couldn't figure out what was wrong so I resorted to my ol' faithfull friend: Google and voila MS has confirmed it to be a bug (http://support.microsoft.com/default.aspx?scid=kb;en-us;Q316495)! (aside: while typing I sowehow lost the entire content after this so have to type in everything again!!, very very bugged!). Googling further I found a workaround on CodeProject, the code basically creates a custom RadioButton which derives from the RadioButton and overrides Render method to ensure that same html name is rendered (using the GroupName property) and implements IPostBackDataHandler to persist the state of attributes on postback. The only problem was that the code was in C# and my current project is on VB.Net, converting C# code to VB.Net or vice-versa is a pretty trivial matter and generally does not take more than a minute per line of code or so I thought! this is where I learn another subtle but very big difference between C# and VB.Net, though C# allows you to re-implement an interface(in my case IPostBackDataHandler) already implemented in the base class (RadioButton), VB.Net does not and generates compile time errors!
(end aside) I really don't understand why such a feature would be only supported in one language and not another when MS is pushing both the languages equally now!! So this option was also ruled out but not to be deterred I thought of inheriting the custom control from one level up than RadioButton and then perhaps I could re-implement the IPostBackDataHandler well no luck, cause RadioButton inherits from CheckBox which actually implemnts the interface, so had to go one more level up: WebControl..now creating a RadioButton by inheriting from WebControl is no trivial matter..that's when suddenly a tool amazing tool called Reflector(www.aisto.com/roeder/dotnet/) came to my mind, basically Reflector allows you to reverse-engineer .Net Assemblies. Downloaded it and installed it and reverse engineered the code for radiobutton and checkbox and with few tweakings here and there, I had my custom radiobutton control ready which were mutually exclusive when placed within the grid or for that matter any container which implements INamingContainer in VB.Net! And am I pleased about my feat, you bet.

No comments:

Post a Comment