📄 supercontentrotator.vb
字号:
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections
Imports System.Data
Namespace Superexpert
'*********************************************************************
'
' SuperContentRotator Class
'
' The SuperContentRotator enables you to randomly display a list of
' content items from a data source. This control supports templates
' through its ItemTemplate property.
'
' See the SuperContentRotator.aspx file for
' samples of using this control.
'
' This control is from the book ASP.NET Unleashed written by
' Stephhen Walther and published by SAMS publishing.
'
'*********************************************************************
Public Class SuperContentRotator
Inherits WebControl
Implements INamingContainer
Private _items As New ArrayList()
Private _dataTextField As String = String.Empty
Private _dataValueField As String = String.Empty
Private _contentFile As String = String.Empty
Private _dataSource As Object
Private _dataMember As String
Private _repeatLayout As RepeatLayout = RepeatLayout.Table
Private _itemTemplate As ITemplate = Nothing
Private _tableStyle As New TableStyle()
Private _itemStyle As New TableItemStyle()
Private _alternatingItemStyle As New TableItemStyle()
'*********************************************************************
'
' ItemCount Property
'
' Represents the number of items from the data source.
'
'*********************************************************************
Public Property ItemCount() As Integer
Get
If ViewState("ItemCount") Is Nothing Then
Return 0
Else
Return ViewState("ItemCount")
End If
End Get
Set
ViewState("ItemCount") = value
End Set
End Property
'*********************************************************************
'
' RepeatItems Property
'
' Represents the number of items to display from the data source.
'
'*********************************************************************
Public Property RepeatItems() As Integer
Get
If ViewState("RepeatItems") Is Nothing Then
Return 1
Else
Return ViewState("RepeatItems")
End If
End Get
Set
ViewState("RepeatItems") = value
End Set
End Property
'*********************************************************************
'
' RepeatLayout Property
'
' Represents whether content items are displayed in an HTML table.
'
'*********************************************************************
Public Property RepeatLayout() As RepeatLayout
Get
Return _repeatLayout
End Get
Set
_repeatLayout = value
End Set
End Property
'*********************************************************************
'
' DataSource Property
'
' The source of the content items. Either this property or
' the ContentFile property must be set.
'
'*********************************************************************
Public Property DataSource() As Object
Get
Return _dataSource
End Get
Set
_dataSource = value
End Set
End Property
'*********************************************************************
'
' DataMember Property
'
' The name of the DataTable when a DataSet is supplied for the
' data source.
'
'*********************************************************************
Public Property DataMember() As String
Get
Return _dataMember
End Get
Set
_dataMember = value
End Set
End Property
'*********************************************************************
'
' ContentFile Property
'
' The virtual path to an XML file that contains content items.
'
'*********************************************************************
Public Property ContentFile() As String
Get
Return _contentFile
End Get
Set
_contentFile = value
End Set
End Property
'*********************************************************************
'
' TableStyle Property
'
' Formats the table displayed around the content items.
'
'*********************************************************************
Public ReadOnly Property TableStyle() As TableStyle
Get
Return _tableStyle
End Get
End Property
'*********************************************************************
'
' ItemStyle Property
'
' Formats each content item.
'
'*********************************************************************
Public ReadOnly Property ItemStyle() As TableItemStyle
Get
Return _itemStyle
End Get
End Property
'*********************************************************************
'
' AlternatingItemStyle Property
'
' Formats every other content item.
'
'*********************************************************************
Public ReadOnly Property AlternatingItemStyle() As TableItemStyle
Get
Return _alternatingItemStyle
End Get
End Property
'*********************************************************************
'
' GetDataSource Method
'
' Calculate the data source.
'
'*********************************************************************
Private Function GetDataSource(dataSource As Object, dataMember As String) As IEnumerable
If TypeOf dataSource Is IEnumerable Then
Return CType(dataSource, IEnumerable)
End If
Dim dst As DataSet = dataSource '
'ToDo: Error processing original source shown below
'
' DataSet dst = dataSource as DataSet;
'--------------------------------------^--- Syntax error: ';' expected
If Not (dst Is Nothing) Then
If dataMember <> String.Empty Then
Return dst.Tables(DataMember).DefaultView
Else
Return dst.Tables(0).DefaultView
End If
Else
Throw New ArgumentException("Invalid data source!")
End If
End Function 'GetDataSource
'*********************************************************************
'
' GetDataSourceFromFile Method
'
' Retrieve a DataSet that represents an XML file.
'
'*********************************************************************
Private Function GetDataSourceFromFile(fileName As String) As IEnumerable
Dim dst As New DataSet()
dst.ReadXml(Page.MapPath(fileName))
Return dst.Tables(0).DefaultView
End Function 'GetDataSourceFromFile
'*********************************************************************
'
' ItemTemplate Property
'
' Represents the ItemTemplate.
'
'*********************************************************************
<TemplateContainer(GetType(ContentItem))> _
Public Property ItemTemplate() As ITemplate
Get
Return _itemTemplate
End Get
Set
_itemTemplate = value
End Set
End Property
'*********************************************************************
'
' OnDataBinding Method
'
' When databind is called, create the control hiearchy from the
' datasource.
'
'*********************************************************************
Protected Overrides Sub OnDataBinding(e As EventArgs)
' Use an enumerator to creates the items collection
Dim objData As IEnumerable = Nothing
If _contentFile <> String.Empty Then
objData = GetDataSourceFromFile(_contentFile)
Else
objData = GetDataSource(_dataSource, _dataMember)
End If
Dim objEnum As IEnumerator = objData.GetEnumerator()
While objEnum.MoveNext()
Dim ctlItem As New ContentItem()
ctlItem.DataItem = objEnum.Current
_items.Add(ctlItem)
End While
' Add all the controls
CreateControlHierarchy(True)
' Prevent CreateChildControls from executing
ChildControlsCreated = True
End Sub 'OnDataBinding
'*********************************************************************
'
' CreateChildControls Method
'
' When databind is not called, create the control hiearchy from
' view state.
'
'*********************************************************************
Protected Overrides Sub CreateChildControls()
CreateControlHierarchy(False)
End Sub 'CreateChildControls
'*********************************************************************
'
' GetRandomContentItem Method
'
' Randomly retrieve one content item and remove it so that
' the same item is not displayed twice.
'
'*********************************************************************
Private Function GetRandomContentItem() As ContentItem
Dim objRan As New Random()
Dim contentIndex As Integer = objRan.Next(_items.Count)
Dim ctlItem As ContentItem = CType(_items(contentIndex), ContentItem)
_items.RemoveAt(contentIndex)
Return ctlItem
End Function 'GetRandomContentItem
'*********************************************************************
'
' CreateControlHierarchy Method
'
' Add all the content items to the ItemTemplate.
'
'*********************************************************************
Private Sub CreateControlHierarchy(useDataSource As Boolean)
Dim ctlItem As ContentItem = Nothing
Dim _displayCount As Integer = 0
' Check for ItemTemplate
If ItemTemplate Is Nothing Then
Throw New Exception("You must supply an ItemTemplate!")
End If
' Update Item Count
If useDataSource Then
ItemCount = _items.Count
End If
' Calculate display count
If RepeatItems = 0 Then
_displayCount = ItemCount
Else
_displayCount = RepeatItems
End If
If _displayCount > ItemCount Then
_displayCount = ItemCount
End If
' Create each item
Dim i As Integer
For i = 0 To _displayCount - 1
If useDataSource Then
ctlItem = GetRandomContentItem()
Else
ctlItem = New ContentItem()
End If
' Assign template specific properties
ctlItem.ItemIndex = i
' Add the item to the ItemTemplate
ctlItem.ItemType = ListItemType.Item
ItemTemplate.InstantiateIn(ctlItem)
' Add the item to the controls collection
Controls.Add(ctlItem)
Next i
End Sub 'CreateControlHierarchy
'*********************************************************************
'
' RenderContents Method
'
' When FlowLayout is table, render an HTML table.
'
'*********************************************************************
Protected Overrides Sub RenderContents(writer As HtmlTextWriter)
' if flow layout mode, use default rendering
If _repeatLayout = RepeatLayout.Flow Then
MyBase.RenderContents(writer)
Return
End If
' Otherwise, render a table
_alternatingItemStyle.MergeWith(_itemStyle)
_tableStyle.AddAttributesToRender(writer)
writer.RenderBeginTag(HtmlTextWriterTag.Table)
Dim i As Integer
For i = 0 To Controls.Count - 1
If i Mod 2 = 0 Then
_itemStyle.AddAttributesToRender(writer)
Else
_alternatingItemStyle.AddAttributesToRender(writer)
End If
writer.RenderBeginTag(HtmlTextWriterTag.Tr)
writer.RenderBeginTag(HtmlTextWriterTag.Td)
Controls(i).RenderControl(writer)
writer.RenderEndTag()
writer.RenderEndTag()
Next i
writer.RenderEndTag()
End Sub 'RenderContents
End Class 'SuperContentRotator
'*********************************************************************
'
' ContentItem Class
'
' Represents a particular content item.
'
'*********************************************************************
Public Class ContentItem
Inherits Control
Implements INamingContainer
Private _dataItem As Object
Private _itemIndex As Integer
Private _itemType As ListItemType
Public Property DataItem() As Object
Get
Return _dataItem
End Get
Set
_dataItem = value
End Set
End Property
Public Property ItemIndex() As Integer
Get
Return _itemIndex
End Get
Set
_itemIndex = value
End Set
End Property
Public Property ItemType() As ListItemType
Get
Return _itemType
End Get
Set
_itemType = value
End Set
End Property
End Class 'ContentItem
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -