• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

VB.NET HierarchicalDataBoundControl类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了VB.NET中System.Web.UI.WebControls.HierarchicalDataBoundControl的典型用法代码示例。如果您正苦于以下问题:VB.NET HierarchicalDataBoundControl类的具体用法?VB.NET HierarchicalDataBoundControl怎么用?VB.NET HierarchicalDataBoundControl使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了HierarchicalDataBoundControl类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。

示例1: PerformDataBinding

' 导入命名空间
Imports System.Collections
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.VB.Controls

    <AspNetHostingPermission(SecurityAction.Demand, _
      Level:=AspNetHostingPermissionLevel.Minimal), _
      AspNetHostingPermission(SecurityAction.InheritanceDemand, _
      Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public Class GeneologyTree
        Inherits HierarchicalDataBoundControl

        Dim MaxDepth As Integer = 0

        Private aRootNode As TreeNode
        Public ReadOnly Property RootNode() As TreeNode
            Get
                If aRootNode Is Nothing Then
                    aRootNode = New TreeNode(String.Empty)
                End If
                Return aRootNode
            End Get
        End Property

        Private alNodes As ArrayList
        Public ReadOnly Property Nodes() As ArrayList
            Get
                If alNodes Is Nothing Then
                    alNodes = New ArrayList()
                End If
                Return alNodes
            End Get
        End Property

        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

        Protected Overrides Sub PerformDataBinding()
            MyBase.PerformDataBinding()

            ' Do not attempt to bind data if there is no
            ' data source set.
            If Not IsBoundUsingDataSourceID AndAlso DataSource Is Nothing Then
                Return
            End If

            Dim view As HierarchicalDataSourceView = GetData(RootNode.DataPath)

            If view Is Nothing Then
                Throw New InvalidOperationException _
                ("No view returned by data source control.")
            End If

            Dim enumerable As IHierarchicalEnumerable = view.Select()
            If Not (enumerable Is Nothing) Then

                Nodes.Clear()

                Try
                    RecurseDataBindInternal(RootNode, enumerable, 1)
                Finally
                End Try
            End If

        End Sub

        Private Sub RecurseDataBindInternal(ByVal node As TreeNode, _
            ByVal enumerable As IHierarchicalEnumerable, _
            ByVal depth As Integer)

            Dim item As Object
            For Each item In enumerable

                Dim data As IHierarchyData = enumerable.GetHierarchyData(item)

                If Not data Is Nothing Then

                    ' Create an object that represents the bound data
                    ' to the control.
                    Dim newNode As New TreeNode()
                    Dim rvnode As New RootViewNode()

                    rvnode.Node = newNode
                    rvnode.Depth = depth

                    ' The dataItem is not just a string, but potentially
                    ' an XML node or some other container. 
                    ' If DataTextField is set, use it to determine which 
                    ' field to render. Otherwise, use the first field.                    
                    If DataTextField.Length > 0 Then
                        newNode.Text = DataBinder.GetPropertyValue _
                        (data, DataTextField, Nothing)
                    Else
                        Dim props As PropertyDescriptorCollection = _
                        TypeDescriptor.GetProperties(data)

                        ' Set the "default" value of the node.
                        newNode.Text = String.Empty

                        ' Set the true data-bound value of the TextBox,
                        ' if possible.
                        If props.Count >= 1 Then
                            If Not props(0).GetValue(data) Is Nothing Then
                                newNode.Text = props(0).GetValue(data).ToString()
                            End If
                        End If
                    End If

                    Nodes.Add(rvnode)

                    If data.HasChildren Then
                        Dim newEnumerable As IHierarchicalEnumerable = _
                            data.GetChildren()
                        If Not (newEnumerable Is Nothing) Then
                            RecurseDataBindInternal(newNode, _
                            newEnumerable, depth + 1)
                        End If
                    End If

                    If MaxDepth < depth Then
                        MaxDepth = depth
                    End If
                End If
            Next item

        End Sub

        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

            writer.WriteLine("<PRE>")
            Dim currentDepth As Integer = 1
            Dim currentTextLen As Integer = 0

            Dim rvnode As RootViewNode
            For Each rvnode In Nodes
                If rvnode.Depth = currentDepth Then
                    Dim output As String = "  " + rvnode.Node.Text + "  "
                    writer.Write(output)
                    currentTextLen = currentTextLen + output.Length
                Else
                    writer.WriteLine("")

                    ' Some very basic white-space formatting.
                    ' The implicit conversion to an Integer is fine, as 
                    ' a general estimate is acceptable for this 
                    ' simple example.
                    Dim halfLine As Integer = CInt(currentTextLen / 2)
                    Dim i As Integer
                    For i = 0 To halfLine
                        writer.Write(" "c)
                    Next i
                    writer.Write("|"c)
                    writer.WriteLine("")
                    currentDepth += 1
                    Dim j As Integer
                    For j = 0 To halfLine
                        writer.Write(" "c)
                    Next j
                    Dim output As String = "  " + rvnode.Node.Text + "  "
                    writer.Write(output)
                    currentTextLen = currentTextLen + output.Length
                End If
            Next rvnode
            writer.WriteLine("</PRE>")

        End Sub


        Private Class RootViewNode
            Public Node As TreeNode
            Public Depth As Integer
        End Class
    End Class
End Namespace
开发者ID:VB.NET开发者,项目名称:System.Web.UI.WebControls,代码行数:193,代码来源:HierarchicalDataBoundControl



注:本文中的System.Web.UI.WebControls.HierarchicalDataBoundControl类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VB.NET ProxyWebPartManager类代码示例发布时间:2022-05-26
下一篇:
VB.NET CustomExpression类代码示例发布时间:2022-05-26
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap