13

如何捕获单击 TreeView 的选定节点的事件?它不会触发SelectedNodeChanged,因为选择显然没有改变,但是我可以捕捉到什么事件,所以我知道选择的节点被点击了?

更新:当我有时间时,我将不得不深入研究 TreeView 控件的内部,并找出它处理单击事件的内容和位置,并将 TreeView 子类化以公开一个新事件 OnSelectedNodeClicked。

我可能会在圣诞节假期这样做,然后我会报告结果。

更新:我在下面提出了一个解决方案,它对 TreeView 控件进行了子类化。

4

9 回答 9

8

最简单的方法 - 如果它不干扰您的其余代码 - 只需将节点设置为未在 SelectedNodeChanged 方法中选择。

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e){
  // Do whatever you're doing
  TreeView1.SelectedNode.Selected = false;
}
于 2008-09-20T05:47:50.953 回答
6

经过一段漫长的时间后,我终于有时间研究如何对 TreeView 进行子类化以处理被单击的选定节点。

这是我的解决方案,它公开了一个新事件SelectedNodeClicked,您可以从页面或任何地方处理该事件。(如果需要,重构为 C# 是一项简单的任务

Imports System.Web.UI
Imports System.Web


Public Class MyTreeView
  Inherits System.Web.UI.WebControls.TreeView

  Public Event SelectedNodeClicked As EventHandler

  Private Shared ReadOnly SelectedNodeClickEvent As Object

  Private Const CurrentValuePathState As String = "CurrentValuePath"

  Protected Property CurrentValuePath() As String
    Get
      Return Me.ViewState(CurrentValuePathState)
    End Get
    Set(ByVal value As String)
      Me.ViewState(CurrentValuePathState) = value
    End Set
  End Property

  Friend Sub RaiseSelectedNodeClicked()

    Me.OnSelectedNodeClicked(EventArgs.Empty)

  End Sub

  Protected Overridable Sub OnSelectedNodeClicked(ByVal e As EventArgs)

    RaiseEvent SelectedNodeClicked(Me, e)

  End Sub

  Protected Overrides Sub OnSelectedNodeChanged(ByVal e As System.EventArgs)

    MyBase.OnSelectedNodeChanged(e)

    ' Whenever the Selected Node changed, remember its ValuePath for future reference
    Me.CurrentValuePath = Me.SelectedNode.ValuePath

  End Sub

  Protected Overrides Sub RaisePostBackEvent(ByVal eventArgument As String)

    ' Check if the node that caused the event is the same as the previously selected node
    If Me.SelectedNode IsNot Nothing AndAlso Me.SelectedNode.ValuePath.Equals(Me.CurrentValuePath) Then
      Me.RaiseSelectedNodeClicked()
    End If

    MyBase.RaisePostBackEvent(eventArgument)

  End Sub

End Class
于 2008-12-21T06:03:14.050 回答
4

存储所选内容并使用 Page_Load 事件处理程序中的代码将所选内容与您存储的内容进行比较。与 SelectedNodeChanged 不同,即使所选值没有改变,每次回发都会调用 Page_Load。

例子

替代文字 http://smithmier.com/TreeViewExample.png

html

<form id="form1" runat="server">
<div>
    <asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"
        ShowLines="True">
        <Nodes>
            <asp:TreeNode Text="Root" Value="Root">
                <asp:TreeNode Text="RootSub1" Value="RootSub1"></asp:TreeNode>
                <asp:TreeNode Text="RootSub2" Value="RootSub2"></asp:TreeNode>
            </asp:TreeNode>
            <asp:TreeNode Text="Root2" Value="Root2">
                <asp:TreeNode Text="Root2Sub1" Value="Root2Sub1">
                    <asp:TreeNode Text="Root2Sub1Sub1" Value="Root2Sub1Sub1"></asp:TreeNode>
                </asp:TreeNode>
                <asp:TreeNode Text="Root2Sub2" Value="Root2Sub2"></asp:TreeNode>
            </asp:TreeNode>
        </Nodes>
    </asp:TreeView>
    <asp:Label ID="Label1" runat="server" Text="Selected"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></div>
</form>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if(TreeView1.SelectedNode!=null && this.TextBox1.Text == TreeView1.SelectedNode.Value.ToString())
    {
        Label2.Text = (int.Parse(Label2.Text) + 1).ToString();
    }
    else
    {
        Label2.Text = "0";
    }
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
    this.TextBox1.Text = TreeView1.SelectedNode.Value.ToString();
}
于 2008-09-20T07:26:06.740 回答
1

When you're adding nodes to the tree in the _TreeNodePopulate() event, set the .SelectionAction property on the node.

TreeNode newCNode;
newCNode = new TreeNode("New Node");

newCNode.SelectAction = TreeNodeSelectAction.Select;

//now you can set the .NavigateUrl property to call the same page with some query string parameter to catch in the page_load()

newCNode.NavigateUrl = "~/ThisPage.aspx?args=" + someNodeAction

RootNode.ChildNodes.Add(newCNode);
于 2008-12-10T19:20:50.470 回答
1

C#:

TreeNode node = TreeTypes.FindNode(obj.CustomerTypeId.ToString());


TreeTypes.Nodes[TreeTypes.Nodes.IndexOf(node)].Select();
于 2012-09-02T07:38:24.963 回答
1
protected void Page_Load(object sender, EventArgs e) 
    {
        if (!IsPostBack)
        {
            TreeView1.SelectedNode.Selected = false;
        }
    }

为我工作

于 2010-12-01T13:35:17.110 回答
0

您始终可以使用 MouseDown 或 MouseUp 事件并检查它是否是选定的节点。

于 2008-09-20T07:06:53.603 回答
0

我使用该ShowCheckBox属性和 Checked 属性来“突出显示”所选项目。当SelectedNodeChanged事件发生时:

  1. 我将旧选择的ShowCheckBox属性和属性设置为 false,并将新选择的属性和属性设置为 true。CheckedShowCheckBoxChecked
  2. 我将所选节点用于任何操作
  3. 最后,我取消选择所选项目: myTreeView.SelecteNode.Selected = false
于 2020-02-28T06:57:33.370 回答
-1

我有一个问题,但我解决了!

在服务器端代码中:

    protected void MainTreeView_SelectedNodeChanged(object sender, EventArgs e)
    {
        ClearTreeView();
        MainTreeView.SelectedNode.Text = "<span class='SelectedTreeNodeStyle'>" + MainTreeView.SelectedNode.Text + "</span>";
        MainTreeView.SelectedNode.Selected = false;

    }

    public void ClearTreeView()
    {
         for (int i = 0; i < MainTreeView.Nodes.Count; i++)
        {
            for(int j=0;j< MainTreeView.Nodes[i].ChildNodes.Count;j++)
            {
                ClearNodeText(MainTreeView.Nodes[i].ChildNodes[j]);
            }
            ClearNodeText(MainTreeView.Nodes[i]);
        }
    }

    public void ClearNodeText(TreeNode tn)
    {
        tn.Text = tn.Text.Replace("<span class='SelectedTreeNodeStyle'>", "").Replace("</span>", "");
    }

在客户端代码中:

 <style type="text/css">
     .SelectedTreeNodeStyle { font-weight: bold;}
 </style>
于 2015-01-12T12:26:56.307 回答