积极答复者
怎样保存用户控件(复合控件)中定义的属性

问题
-
我想在用户控件中写保存状态逻辑,而不是引用页中用视图状态和session之类的东西,以下代码count属性的值不能累加。 之所以这么做因为这更复合OO编程的标准。
用户控件代码==================================
Partial Class WebUserControl
Inherits System.Web.UI.UserControlPrivate intCount As Integer
Public Property count() As Integer
Get
Return intCount
End Get
Set(ByVal value As Integer)
intCount = value
End Set
End PropertyProtected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
intCount += 1
End Sub
End Class引用页代码===================================================
Partial Class _Default
Inherits System.Web.UI.PageProtected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
Label1.Text = WebUserControl1.count
End Sub
End Class
答案
-
终于搞定了。 在用户控件中用视图状态也是可以的,呵呵。
Partial Class WebUserControl
Inherits System.Web.UI.UserControl
Public Property count() As String
Get
Return CInt(ViewState("test"))
End Get
Set(ByVal value As String)
ViewState("test") = value
End Set
End PropertyProtected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As String = CInt(ViewState("test"))
a += 1
ViewState("test") = a
End Sub
End Class- 已标记为答案 网络游侠 2010年5月11日 13:23
-
问题彻底解决,我用重写了用户控件的 SaveControlState和LoadControlState,这种情况是是完美的,呵呵。
Private intCount As IntegerPublic Property count() As Integer
Get
Return intCount
End Get
Set(ByVal value As Integer)
intCount = value
End Set
End PropertyProtected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
intCount += 1
End Sub
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
Page.RegisterRequiresControlState(Me)
MyBase.OnInit(e)
End SubProtected Overrides Function SaveControlState() As Object
Return CType(intCount, Object)
End FunctionProtected Overrides Sub LoadControlState(ByVal savedState As Object)
intCount = CInt(savedState)
End Sub- 已标记为答案 网络游侠 2010年5月11日 13:23
全部回复
-
终于搞定了。 在用户控件中用视图状态也是可以的,呵呵。
Partial Class WebUserControl
Inherits System.Web.UI.UserControl
Public Property count() As String
Get
Return CInt(ViewState("test"))
End Get
Set(ByVal value As String)
ViewState("test") = value
End Set
End PropertyProtected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As String = CInt(ViewState("test"))
a += 1
ViewState("test") = a
End Sub
End Class- 已标记为答案 网络游侠 2010年5月11日 13:23
-
问题彻底解决,我用重写了用户控件的 SaveControlState和LoadControlState,这种情况是是完美的,呵呵。
Private intCount As IntegerPublic Property count() As Integer
Get
Return intCount
End Get
Set(ByVal value As Integer)
intCount = value
End Set
End PropertyProtected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
intCount += 1
End Sub
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
Page.RegisterRequiresControlState(Me)
MyBase.OnInit(e)
End SubProtected Overrides Function SaveControlState() As Object
Return CType(intCount, Object)
End FunctionProtected Overrides Sub LoadControlState(ByVal savedState As Object)
intCount = CInt(savedState)
End Sub- 已标记为答案 网络游侠 2010年5月11日 13:23