[ASP.NET]維護自訂控制項狀態

[ASP.NET]維護自訂控制項狀態

自定控制項Web Custom Control依照實作方式有下列三種

種類

繼承

其它步驟

全新(html based)

WebControl

Override Render Content

擴充

指定Control Class(i.e Button)

加入所需功能

組合式

CompositeControl

1. 加入要組合的控制項宣告為Field並初始化(new)

2. Override Createchild

Controls(加入組態logic,編排,行為定義)

這次練習如何維護自訂控制項狀態

1. 加入ASP.NET伺服器控制項

clip_image002

2. 撰寫控制項的程式碼並編譯


Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


<DefaultProperty("Text"), ToolboxData("<{0}:MyClock runat=server></{0}:MyClock>")> _
Public Class MyClock
    Inherits Label
    Private _currentcount As Integer = 0 '計數器
    Private receiveitem As New MySendObj() '要暫存的物件
    Private _settime As DateTime '時間
    Private _name As String '姓名
    Private _money As Decimal '金額
    Public Property currentcount As Integer
        Get
            Return _currentcount
        End Get
        Set(ByVal value As Integer)
            _currentcount = value
        End Set
    End Property
    Public Property settime As DateTime
        Get
            Return _settime
        End Get
        Set(ByVal value As DateTime)
            _settime = value
        End Set
    End Property
    Public Property name As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
    Public Property money As Decimal
        Get
            Return _money
        End Get
        Set(ByVal value As Decimal)
            _money = value
        End Set
    End Property
    '儲存control state到keepstate list
    Protected Overrides Function SaveControlState() As Object
        If receiveitem Is Nothing Then
            Return Nothing
        Else
            receiveitem.count = _currentcount
            receiveitem.settime = _settime
            receiveitem.name = _name
            receiveitem.money = _money
            Return CType(receiveitem, Object)
        End If
    End Function
    '讀取control sate到控制項
    Protected Overrides Sub LoadControlState(ByVal savedState As Object)
        If savedState IsNot Nothing Then
            receiveitem = CType(savedState, MySendObj)
            _currentcount = receiveitem.count
            _settime = receiveitem.settime
            _name = receiveitem.name
            _money = receiveitem.money
        End If
    End Sub
    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)
        Page.RegisterRequiresControlState(Me)
    End Sub     
End Class
'自定義物件暫存control state
<Serializable()> Public Class MySendObj
    Private _count As Integer
    Private _settime As DateTime
    Private _name As String
    Private _money As Decimal
    Public Property count As Integer
        Get
            Return _count
        End Get
        Set(ByVal value As Integer)
            _count = value
        End Set
    End Property
    Public Property settime As DateTime
        Get
            Return _settime
        End Get
        Set(ByVal value As DateTime)
            _settime = value
        End Set
    End Property
    Public Property name As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
    Public Property money As Decimal
        Get
            Return _money
        End Get
        Set(ByVal value As Decimal)
            _money = value
        End Set
    End Property
End Class

3. 建立一個空白網站並將控制項拉入

clip_image004

4. 修改網頁的程式碼


    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        MyClock1.name = "李阿狗"
        MyClock1.settime = DateTime.Now
        MyClock1.money = Decimal.Parse("100")
        MyClock1.currentcount += 1
        Label1.Text = MyClock1.name
        Label2.Text = MyClock1.settime.ToLongTimeString()
        Label3.Text = MyClock1.money.ToString()
        Label4.Text = MyClock1.currentcount.ToString()

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        
    End Sub
    
End Class

5. 執行結果,可以看到每個屬性狀態都正確的被載入了

當第一次載入頁面

clip_image006

按下button

clip_image008

注意事項:

請將viewstate關閉

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" EnableViewState="false" %>