代码之家  ›  专栏  ›  技术社区  ›  Mike Florian Doyen

我可以将带有事件的按钮添加到自定义树节点吗?

  •  1
  • Mike Florian Doyen  · 技术社区  · 15 年前

    我扩展了treeview、treenode和nodeType,这样我就可以拥有自定义节点。某些节点上有图像按钮,允许它们添加子节点或删除节点。我无法处理按钮上的任何事件。

    Public Class ContentTreeView
    Inherits TreeView
    
    Public Event OnAddChild(ByVal sender As Object, ByVal e As EventArgs)
    Public Event OnDelete(ByVal sender As Object, ByVal e As EventArgs)
    
    Private _AddImageURL As String = String.Empty
    Private _DeleteImageURL As String = String.Empty
    
    Public Property AddImageURL() As String
        Get
            Return _AddImageURL
        End Get
        Set(ByVal value As String)
            _AddImageURL = value
        End Set
    End Property
    
    Public Property DeleteImageURL() As String
        Get
            Return _DeleteImageURL
        End Get
        Set(ByVal value As String)
            _DeleteImageURL = value
        End Set
    End Property
    
    Protected Overrides Function CreateNode() As TreeNode
        Dim retval As ContentTreeNode = New ContentTreeNode(AddImageURL, DeleteImageURL)
        AddHandler retval.OnAddChild, AddressOf ContentNode_AddChild
        AddHandler retval.OnDelete, AddressOf ContentNode_Delete
        Return retval
    End Function
    
    Protected Sub ContentNode_AddChild(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent OnAddChild(Nothing, Nothing)
    End Sub
    Protected Sub ContentNode_Delete(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent OnDelete(Nothing, Nothing)
    End Sub
    
    Public Class ContentTreeNode
        Inherits TreeNode
    
        Public Event OnAddChild(ByVal sender As Object, ByVal e As EventArgs)
        Public Event OnDelete(ByVal sender As Object, ByVal e As EventArgs)
    
        Private _AddImageURL As String = String.Empty
        Private _DeleteImageURL As String = String.Empty
    
        Private btnAddChild As ImageButton
        Private btnDelete As ImageButton
    
        Public Sub New(ByVal AddImageURL_ As String, ByVal DeleteImageURL_ As String)
            _AddImageURL = AddImageURL_
            _DeleteImageURL = DeleteImageURL_
        End Sub
    
        Public Property AddImageURL() As String
            Get
                Return _AddImageURL
            End Get
            Set(ByVal value As String)
                _AddImageURL = value
            End Set
        End Property
    
        Public Property DeleteImageURL() As String
            Get
                Return _DeleteImageURL
            End Get
            Set(ByVal value As String)
                _DeleteImageURL = value
            End Set
        End Property
    
        Protected Overrides Sub RenderPreText(ByVal writer As HtmlTextWriter)
        End Sub
    
        Protected Overrides Sub RenderPostText(ByVal writer As HtmlTextWriter)
            CreateChildControls()
            If GetTreeNodeType() <> ContentTreeNodeTypes.Root Then
                btnAddChild.RenderControl(writer)
                If GetTreeNodeType() <> ContentTreeNodeTypes.Category Then
                    btnDelete.RenderControl(writer)
                End If
            End If
        End Sub
    
        Private Function GetTreeNodeType() As TreeNodeTypes
            Dim leaf As TreeNodeTypes = TreeNodeTypes.Leaf
            If ((Me.Depth = 0) AndAlso (Me.ChildNodes.Count > 0)) Then
                Return ContentTreeNodeTypes.Root
            End If
            If Me.Depth = 1 Then
                Return ContentTreeNodeTypes.Category
            End If
            If ((Me.ChildNodes.Count <= 0) AndAlso Not Me.PopulateOnDemand) Then
                Return leaf
            End If
            Return ContentTreeNodeTypes.Parent
        End Function
    
        Protected Sub CreateChildControls()
            'Controls.Clear()
    
            '***Creat Add Button***
            btnAddChild = New ImageButton()
            btnAddChild.ID = "btnAddChild"
            btnAddChild.ImageUrl = AddImageURL
            btnAddChild.ToolTip = "Add Child"
            AddHandler btnAddChild.Click, AddressOf btnAddChild_Click
    
            '***Create DeleteButton***
            btnDelete = New ImageButton()
            btnDelete.ID = "btnDelete"
            btnDelete.ImageUrl = DeleteImageURL()
            btnDelete.ToolTip = "Delete Page"
            AddHandler btnDelete.Click, AddressOf btnDelete_Click
    
            ''***Add Controls***
            'Me.Controls.Add(btnAddChild)
            'Me.Controls.Add(btnDelete)
        End Sub
    
        Protected Sub btnAddChild_Click(ByVal sender As Object, ByVal e As EventArgs)
            RaiseEvent OnAddChild(Nothing, Nothing)
        End Sub
        Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
            RaiseEvent OnDelete(Nothing, Nothing)
        End Sub
    
    
        Public Enum ContentTreeNodeTypes
            All = 7
            Leaf = 4
            None = 0
            Parent = 2
            Root = 1
            Category = 3
        End Enum
    End Class
    

    端类

    1)我可以实现类似ipostbackEventHandler的东西吗? 2)这是否可能,因为Treenode不是控件/WebControl?

    感谢您的帮助…谢谢!!!!

    2 回复  |  直到 15 年前
        1
  •  0
  •   Vinz    15 年前

    我认为这个问题与时间有关,这意味着孩子们的控制被添加到 ASP.Net page lifecycle 为要执行的事件。

    这可以解决以下问题:

    1. 尽早组成整个树结构,例如 Init 页面事件。
    2. 附加子项 ImageButton 在ContentTreeNode的构造函数中。

    或者,您可以使用JavaScript上下文菜单,这样就不需要将任何子控件附加到 TreeNode ……

        2
  •  0
  •   Mike Florian Doyen    15 年前

    看完这个之后 post . 我决定使用以下解决方案。首先,我将CreateChildControls方法更改为:

            Protected Sub CreateChildControls()
            Dim page As Page = HttpContext.Current.CurrentHandler
            Dim csm As ClientScriptManager = page.ClientScript
            Dim control As WebControl = page.Master.FindControl(_ContainerID).FindControl(_ContentTreeViewID)
    
            '***Creat Add Button***
            btnAddChild = New ImageButton()
            btnAddChild.ID = "btnAddChild"
            btnAddChild.ImageUrl = AddImageURL
            btnAddChild.ToolTip = "Add Child"            
            btnAddChild.Attributes.Add("onClick", csm.GetPostBackEventReference(control, String.Format("{0}:{1}", Me.Value, "Add")))
    
            '***Create DeleteButton***
            btnDelete = New ImageButton()
            btnDelete.ID = "btnDelete"
            btnDelete.ImageUrl = DeleteImageURL()
            btnDelete.ToolTip = "Delete Page"
            btnDelete.Attributes.Add("onClick", csm.GetPostBackEventReference(control, String.Format("{0}:{1}", Me.Value, "Delete")))
        End Sub
    

    然后我必须在自定义树视图中实现IPostbackEventHandler来处理回发事件。也许不是最好的解决方案,但它与自定义事件参数一起工作得很好。