Vb6“标签"在 ASP.Net 中等效的属性? [英] Vb6 "Tag" property equivalent in ASP.Net?

查看:19
本文介绍了Vb6“标签"在 ASP.Net 中等效的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

回到旧的 VB6 时代,所有控件中都有一个名为Tag"的属性,这是一种存储与控件相关的自定义信息的有用方法.每个控件都有它,一切都很幸福......

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.

我能想到一百万种替代方案(显然从模块级字典开始),但没有一个像好的 ol' Tag 那样干净.

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.

我知道我的 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 中.这当然意味着您的标签暴露在发送给用户的页面标记中(尽管以混淆形式),不幸的是,需要一些反射,因为 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.

因此,除非您确实喜欢在代码审查期间引起注意:

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 属性,然后更改您的代码隐藏 (.aspx.vb) 类以继承它而不是 Web.UI.页面,例如:

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“标签"在 ASP.Net 中等效的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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