ASP.Net中的Vb6“Tag”属性等价物?
|
我在这里寻找想法和意见,而不是“真正的答案”,我猜…… 回到旧的VB6时代,在所有控件中都有一个名为“Tag”的属性,这是存储与控件相关的自定义信息的有用方法.每一个控制都拥有它,而且一切都很幸福…… 现在,在.Net(至少对于WebForms),它不再存在了…… 有人有一个很好的替代品吗? 我经常发现这个问题,我在生命周期的不同时间运行不同的函数,并且他们使用我的控件进行操作,并且我希望将它们保持原样,但是应该将信息传递给另一个关于具体控制. 我可以想到一百万个替代品(显然是从模块级字典开始),但没有一个像好的标签一样干净. (注意:我知道我可以将所有控件子类化并使用我的版本.我宁愿不这样做) 有什么建议? 编辑:我正在寻找内部请求,而不是内部请求.我不需要这些信息仍然在PostBack上.例如,这是在_Load和_PreRender方法之间. 编辑2:我知道我的ASp.Net,我确实知道桌面和网络之间的区别,伙计们!我只是试图使用.Net给我的最大化的抽象.我理解这些权衡,相信我,请假设我这样做. 解决方法不,没有直接的等价物,但如果您使用的是框架的v3.5,则可以使用扩展方法轻松添加此功能.例如: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页面中,首先将扩展名放入范围,例如: Imports WebApplication1.Extensions …然后根据需要使用它的控件: TextBox1.SetTag("Test")
Label1.Text = TextBox1.GetTag
LATER EDIT:如果你真的,真的不想将你的标签存储在Session对象中,那么可以将它们填充到Viewstate中.这当然意味着您的标签将在发送给用户的页面标记中公开(尽管是以混淆的形式),并且不幸的是,需要一些反射,因为页面的ViewState属性被标记为“受保护” ‘ 由于某些原因. 因此,此代码几乎只能用于娱乐目的,除非您真的喜欢在代码审查期间引起注意: <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-Behind(.aspx.vb)类来继承它而不是Web.UI.页面,例如: 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
现在,在您的扩展模块中,您可以访问此新定义的属性: Private Function ViewState() As Web.UI.StateBag Return DirectCast(HttpContext.Current.Handler,PageEx).ViewStateEx End Function 仍然有点黑客,但比使用反射更可接受…… (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
