ASP.NET TreeView and Selecting the Selected Node

后端 未结 9 1544
無奈伤痛
無奈伤痛 2020-12-14 11:02

How do I capture the event of the clicking the Selected Node of a TreeView? It doesn\'t fire the SelectedNodeChanged since the selection has obviously not c

相关标签:
9条回答
  • 2020-12-14 11:38

    c#:

    TreeNode node = TreeTypes.FindNode(obj.CustomerTypeId.ToString());
    
    
    TreeTypes.Nodes[TreeTypes.Nodes.IndexOf(node)].Select();
    
    0 讨论(0)
  • 2020-12-14 11:40

    Store what is selected and use code in the Page_Load event handler to compare what is selected to what you have stored. Page_Load is called for every post back even if the selected value doesn't change, unlike SelectedNodeChanged.

    Example

    alt text 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();
    }
    
    0 讨论(0)
  • 2020-12-14 11:42
    protected void Page_Load(object sender, EventArgs e) 
        {
            if (!IsPostBack)
            {
                TreeView1.SelectedNode.Selected = false;
            }
        }
    

    works for me

    0 讨论(0)
提交回复
热议问题