VB6"标签"财产相当于在ASP.Net? [英] Vb6 "Tag" property equivalent in ASP.Net?

查看:198
本文介绍了VB6"标签"财产相当于在ASP.Net?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找的想法和意见在这里,而不是一个真正的答案,我猜...

I'm looking for ideas and opinions here, not a "real answer", I guess...

回到旧的VB6天,有此属性称为所有控件变量,这是存储与控制自定义信息的有效途径。每一个控制有它,一切都幸福......

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

现在,在.net中(至少WebForms的),它不存在了......

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?

编辑:我在寻找的东西帧内请求,而不是国米请求。我不需要这些信息仍然存在一个回发。这是_Load和_ preRender方法之间,例如

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:我知道我的ASp.Net和我认识的桌面和网络,球员之间的区别!我只是尝试使用的.Net给了我最大的抽象。我了解权衡,相信我,请回答假设我做的。

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.

推荐答案

没有,没有直接等同,但如果您使用的框架v3.5版本,则可以使用扩展方法很容易添加此功能。例如:

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

然后,在你的ASP.NET页面,首先把你的扩展到范围,例如:

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

以后编辑:如果你真的,真的不希望将自己的代码存储在Session对象中,有可能将它们塞进你的视图状态代替。当然,这将意味着你的标签在发送给用户的页面标记被曝光(尽管是以混淆的形式),不幸的是,一些反射福是必需的,因为ViewState属性出于某种原因为受保护页面标记。

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

所以,这code应该pretty多只考虑出于娱乐的目的,除非你真的期间code评论引人侧目:

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

最后编辑(我保证......)。而这里的摆脱反射的方法:第一,创建一个新类与可用的保护级别揭露ViewState属性,然后更改您的code-背后(.aspx.vb)类来继承,而不是网络的.UI.Page,例如:

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

这篇关于VB6&QUOT;标签&QUOT;财产相当于在ASP.Net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆