I’m looking for ideas and opinions here, not a ‘real answer’, I guess…
Back in the old VB6 days, there was this property called ‘Tag’ in all controls, that was a useful way to store custom information related to a control. Every single control had it, and all was bliss…
Now, in .Net (at least for WebForms), it’s not there anymore…
Does anyone have a good replacement for that?
I find this problem very often, where I have different functions that run at different times in the lifecycle, and they do stuff with my controls, and I want to keep them as separate as they are, but one should pass information to the other about specific controls.
I can think of a million alternatives (starting with a module-level dictionary, obviously), but none as clean as the good ol’ Tag.
(NOTE: I know I can subclass ALL the controls and use my version instead. I’d rather not)
Any suggestions? How do you solve this normally? Any ideas on why they removed this i the first place?
EDIT: I’m looking for something Intra-Request, not Inter-Request. I don’t need this information to still be there on a PostBack. This is between the _Load and the _PreRender methods, for example.
EDIT2: I DO know my ASp.Net and I do know the difference between the desktop and the web, guys!. I ‘m just trying to use the abstraction that .Net gives me to the maximum. I understand the tradeoffs, believe me, and please answer assuming that I do.
No, there’s no direct equivalent, but if you’re using v3.5 of the Framework, you can add this functionality quite easily using an extension method. For example:
Then, in your ASP.NET pages, first bring your extensions into scope, e.g:
…and then use it your controls as desired:
LATER EDIT: and if you really, really don’t want to store your tags in the Session object, it’s possible to stuff them into your Viewstate instead. This will of course mean that your tags will be exposed in the page markup sent to the user (albeit in obfuscated form), and, unfortunately, that some reflection-fu is required, since the ViewState property of a Page is marked as ‘protected’ for some reason.
So, this code should pretty much be considered for entertainment purposes only, unless you actually like to raise eyebrows during code reviews:
FINAL EDIT (I promise…). And here’s a way to get rid of the reflection: first, create a new class to expose the ViewState property with a usable protection level, then change your Code-Behind (.aspx.vb) classes to inherit that instead of Web.UI.Page, e.g.:
Now, in your extensions module, you can access this newly defined property as:
Still a bit of a hack, but much more acceptable than using reflection…