views:

995

answers:

7

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.

A: 

You can add attributes to some controls, but that seems to add some nasty HTML around the rendered control, something like <div attrName="attrValue">... (it might be a span though)

harriyott
Yep, I don't want the Tag going out though. It's an internal thingy.Plus, that's invalid HTML later (I don't care ASP.Net does it all the time anyway, but still)
Daniel Magliola
Exactly why I don't use them!
harriyott
A: 

I'm not sure what the tag property did in VB6, but maybe you're looking for the Attributes property of Web controls:

MyImgCtrl.Attributes["myCustomTagAttribute"] = "myVal";
Terrapin
Nope. attributes output to the HTML. I want something internal.
Daniel Magliola
A: 

You asked about ASP.Net, but vb6 was a desktop language, and so were it's 'tagged' controls. The VBScript used for classic asp didn't really have a concept of a control.

Now in .Net, for desktop controls you can use inheritance, which is much more powerful than the old Tag property anyway. Inheritance applies to web controls as well, though you do have to be careful about using it to hold state.

Joel Coehoorn
Yep, and then you have to SubClass every single control you make. Not cool.I get the difference between web and desktop, and ASP.Net is very desktop like, and DOES have controls. They should've added Tag there.
Daniel Magliola
No, you only have to subclass controls where you want to use the tag. You should probably be subclassing more anyway.
Joel Coehoorn
A: 

You can use asp:Hidden controls to store data between posts. Like will says, there's no sense to have a tag property if you loose it's value.

Eduardo Campañó
Ugh, even uglier.And again, they could've serialized to Viewstate, no need to lose it. And there IS value to me, even if you lose it.
Daniel Magliola
Please try to explain better what you want first
Eduardo Campañó
+3  A: 

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:

Imports System.Runtime.CompilerServices

Public Module Extensions
  <Extension()> _
  Public Sub SetTag(ByVal ctl As Control, ByVal tagValue As String)
    If SessionTagDictionary.ContainsKey(TagName(ctl)) Then
        SessionTagDictionary(TagName(ctl)) = tagValue
    Else
        SessionTagDictionary.Add(TagName(ctl), tagValue)
    End If
  End Sub

  <Extension()> _
  Public Function GetTag(ByVal ctl As Control) As String
    If SessionTagDictionary.ContainsKey(TagName(ctl)) Then
        Return SessionTagDictionary(TagName(ctl))
    Else
        Return String.Empty
    End If
  End Function

  Private Function TagName(ByVal ctl As Control) As String
    Return ctl.Page.ClientID & "." & ctl.ClientID
  End Function

  Private Function SessionTagDictionary() As Dictionary(Of String, String)
    If HttpContext.Current.Session("TagDictionary") Is Nothing Then
        SessionTagDictionary = New Dictionary(Of String, String)
        HttpContext.Current.Session("TagDictionary") = SessionTagDictionary
    Else
        SessionTagDictionary = DirectCast(HttpContext.Current.Session("TagDictionary"), _ 
          Dictionary(Of String, String))
    End If
  End Function
End Module

Then, in your ASP.NET pages, first bring your extensions into scope, e.g:

Imports WebApplication1.Extensions

...and then use it your controls as desired:

TextBox1.SetTag("Test")

Label1.Text = TextBox1.GetTag

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:

<Extension()> _
  Public Sub SetTag(ByVal ctl As Control, ByVal tagValue As String)
    ViewState.Add(ctl.ID & "_Tag", tagValue)
  End Sub

<Extension()> _
  Public Function GetTag(ByVal ctl As Control) As String
    Return ViewState(ctl.ID & "_Tag")
  End Function

Private Function ViewState() As Web.UI.StateBag
    Return HttpContext.Current.Handler.GetType.InvokeMember("ViewState", _
                Reflection.BindingFlags.GetProperty + _
                Reflection.BindingFlags.Instance + _
                Reflection.BindingFlags.NonPublic, _
                Nothing, HttpContext.Current.CurrentHandler, Nothing)
End Function

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.:

Public Class PageEx
  Inherits System.Web.UI.Page

  Friend ReadOnly Property ViewStateEx() As Web.UI.StateBag
    Get
        Return MyBase.ViewState
    End Get
  End Property
End Class

Now, in your extensions module, you can access this newly defined property as:

Private Function ViewState() As Web.UI.StateBag
  Return DirectCast(HttpContext.Current.Handler, PageEx).ViewStateEx
End Function

Still a bit of a hack, but much more acceptable than using reflection...

mdb
Wow. That's VERY close to what I was asking for and assuming there was no way to do.I'm not very happy about the Session thing, but i'll definitely have to learn this Extensions thing (i'm using v2.0, but NOW i'll migrate :-D )
Daniel Magliola
That's pretty slick, actually!
Bob King
Nice! I'd love to give you more points for that!Hopefully they'll implement the karma passing they're talking about...
Daniel Magliola
Now, is there any way to make this available at design time in the aspx page, so the tag can be set like other properties without doing it in the codebehind??
tbone
tbone: no, unfortunately, extension methods are only available at runtime. I guess if you really wanted to, you could achieve this using a VS.NET plugin or somesuch, but that's more trouble than it's worth
mdb
A: 

The Tag property was always a weird wart-like thing that was useful as something off of which you could hang any data you wanted. But it wasn't strongly-typed and it didn't really make for a coherent design. The property itself just hung off the control like a weird knob. They kept it in WinForms to facilitate porting code over from VB6. The new WPF Control class doesn't have a Tag.

With .NET you have full object-orientation and adequate type polymorphism, so you can strongly-type whatever extra information you want to associate with a code, whether it's in a subclass or a Dictionary<Control,TValue>.

If it's in a single Page request and you want a general solution a Dictionary in the Page itself for each value type (e.g., Dictionary<Control,string> and Dictionary<Control,BusinessObject>) should be exactly what you need.

Mark Cidade
Sure. But that's much uglier than the Tag, seriously.I get all you're saying and I agree, but it was still very useful. You are putting the data associated right where it should be, instead of on a completely out of the way "global" variable.
Daniel Magliola
A: 

You can also use the composite pattern instead of using inheritence:

public class TaggedControl<TControl, TTag> : Control 
 where TControl : Control, new()
 { public TaggedControl() {this.Control= new TControl();}

   public TControl Control {get; private set;}
   public TTag     Tag     {get; set;}     

   protected override void CreateChildControls(){Controls.Add(Control);}
 }

 var textBox = new TaggedControl<TextBox, string>();
 textBox.Tag = "Test";
 label.Text  = textBox.Tag;
Mark Cidade