2 Sept 2011

DataBoundCustom Control

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections.Generic

Namespace Samples.AspNet.Controls.VB
    <AspNetHostingPermission(SecurityAction.Demand, _
        Level:=AspNetHostingPermissionLevel.Minimal), _
        AspNetHostingPermission(SecurityAction.InheritanceDemand, _
        Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public Class SimpleDataBoundColumn
        Inherits DataBoundControl


        Dim chkBoxList As New CheckBoxList

        Public Property DataTextField() As String
            Get
                Dim o As Object = ViewState("DataTextField")
                If o Is Nothing Then
                    Return String.Empty
                Else
                    Return CStr(o)
                End If
            End Get
            Set(ByVal value As String)
                ViewState("DataTextField") = value
                If (Initialized) Then
                    OnDataPropertyChanged()
                End If
            End Set
        End Property
        Public Property DataValueField() As String
            Get
                Dim o As Object = ViewState("DataValueField")
                If o Is Nothing Then
                    Return String.Empty
                Else
                    Return CStr(o)
                End If
            End Get
            Set(ByVal value As String)
                ViewState("DataValueField") = value
                If (Initialized) Then
                    OnDataPropertyChanged()
                End If
            End Set
        End Property

        Protected Overrides Sub PerformSelect()
            ' Call OnDataBinding here if bound to a data source using the
            ' DataSource property (instead of a DataSourceID), because the
            ' databinding statement is evaluated before the call to GetData.      
            If Not IsBoundUsingDataSourceID Then
                OnDataBinding(EventArgs.Empty)
            End If

            ' The GetData method retrieves the DataSourceView object from 
            ' the IDataSource associated with the data-bound control.           
            GetData().Select(CreateDataSourceSelectArguments(), _
                AddressOf OnDataSourceViewSelectCallback)

            ' The PerformDataBinding method has completed.
            RequiresDataBinding = False
            MarkAsDataBound()

            ' Raise the DataBound event.
            OnDataBound(EventArgs.Empty)

        End Sub

        Private Sub OnDataSourceViewSelectCallback(ByVal retrievedData As IEnumerable)
            ' Call OnDataBinding only if it has not already been
            ' called in the PerformSelect method.
            If IsBoundUsingDataSourceID Then
                OnDataBinding(EventArgs.Empty)
            End If
            ' The PerformDataBinding method binds the data in the 
            ' retrievedData collection to elements of the data-bound control.
            PerformDataBinding(retrievedData)

        End Sub

        Protected Overrides Sub PerformDataBinding(ByVal retrievedData As IEnumerable)
            MyBase.PerformDataBinding(retrievedData)

            ' Verify data exists.
            If Not (retrievedData Is Nothing) Then

                Dim li As ListItem
                Dim dataStr As String = String.Empty
                Dim dataVal As String = String.Empty

                Dim dataItem As Object
                For Each dataItem In retrievedData
                    ' If the DataTextField was specified get the data
                    ' from that field, otherwise get the data from the first field.
                    If DataTextField.Length > 0 Then
                        dataStr = DataBinder.GetPropertyValue(dataItem, DataTextField, Nothing)
                        dataVal = DataBinder.GetPropertyValue(dataItem, DataValueField, Nothing)
                    Else
                        Dim props As PropertyDescriptorCollection = TypeDescriptor.GetProperties(dataItem)
                        If props.Count >= 2 Then
                            If Nothing <> props(0).GetValue(dataItem) Then
                                dataStr = props(0).GetValue(dataItem).ToString()
                                dataVal = props(1).GetValue(dataItem).ToString()
                            End If
                        End If
                    End If

                    li = New ListItem
                    li.Text = dataStr
                    li.Value = dataVal
                    chkBoxList.Items.Add(li)
                    chkBoxList.ID = "chkPC"
                    chkBoxList.CellPadding = 0
                    chkBoxList.CellSpacing = 0
                    chkBoxList.Width = Unit.Percentage(100)
                Next dataItem

                Controls.Add(chkBoxList)

            End If

        End Sub

        Public ReadOnly Property Selected() As ListItemCollection
            Get
                Dim ret As New ListItemCollection
                For Each li As ListItem In chkBoxList.Items
                    If li.Selected Then
                        ret.Add(li)
                    End If
                Next
                Return ret
            End Get
        End Property
    End Class
End Namespace

No comments:

Post a Comment