The Treeview navigation link on the ASP. NET master page

Source: Internet
Author: User

Recently, I have been searching for "ASP. net master page Treeview navigation link issue "related information, many people have encountered similar issues with me, so I took some time to debug this problem myself.

First, describe my problem:

When the Treeview control is placed on the master page, the menu status of the Treeview control remains the initial state when the page navigation link is made, it does not remain in the clicked status after a page Jump is clicked.

The simplest way is to use the framework. Although the problem is easy to solve, I still want to think about it, so I wrote a debugging program to share it. The program is rough and can achieve the desired effect. So I went to msdn to find the relevant information about the Treeview control, and then combined with my own ideas, finally let me write it out.

Idea: In the Treeview control, I only set two levels (if the level is increased, I may have to handle it again ). When we click a parent node, we store the current parent node in session and click the child node to determine whether the parent node of the node is consistent with the parent node of the session, expand the child level of the parent node, and skip the step. (It is best to use foreach to traverse the navigation menu)

The most important thing in the program is to understand the selectaction attribute of the Treeview control. This attribute involves some control-related events, as shown below:

1. treenodeselectaction. Expand:

Switches nodes between the expand and collapse statuses. Trigger the treenodeexpanded event or treenodecollapsed event accordingly.

2. treenodeselectaction. None:

No events are triggered when a node is selected.

3. treenodeselectaction. Select:

The selectednodechanged event is triggered when a node is selected.

4. treenodeselectaction. selectexpand:

The selectednodechanged and treenodeexpanded events are triggered when a node is selected. Nodes only expand and not collapse

In my demo, I wrote the first and two associated events. The Code is as follows:

Trigger the following three events on the master page:

 

Pageload event on the master page
1 protected void page_load (Object sender, eventargs E)
2 {
3 if (! Ispostback)
4 {
5 // verify whether there are nodes in the current session
6 if (session ["treenode"]! = NULL)
7 {
8 treenode parentnode = session ["treenode"] As treenode;
9 // traverse the nodes set of the Treeview
10 foreach (treenode tn in this. treeview1.nodes)
11 {
12 // if the text of the cyclic node matches the text of the current node, expand the node; otherwise fold
13 if (TN. Text. Trim () = parentnode. Text. Trim ())
14 {
15 tn. Expand ();
16}
17 else
18 {
19 tn. Collapse ();
20}
21
22}
23}
24}
25}

 

Treeview expand event
1 protected void treeviewinclutreenodeexpanded (Object sender, treenodeeventargs E)
2 {// get the current node
3 treenode currentnode = E. node;
4 // verify whether there are nodes in the current session
5 If (session ["treenode"]! = NULL)
6 {
7 treenode node = session ["treenode"] As treenode;
8 // traverse the nodes set of the Treeview
9 foreach (treenode tn in this. treeview1.nodes)
10 {
11 // if the text of the cyclic node does not match the text of the current node, fold the remaining nodes
12 if (TN. Text. Trim ()! = Currentnode. Text. Trim ())
13 {
14 tn. Collapse ();
15}
16}
17
18
19}
20 // store the current node
21 session ["treenode"] = currentnode;
22}

Treeview collapse event

1 protected void treeviewinclutreenodecollapsed (Object sender, treenodeeventargs E)
2 {
3 // remove the stored Node
4 session. Remove ("treenode ");
5}

In addition, I also thought that it would be more appropriate to implement a multi-level menu through dynamic binding. When traversing a node, I think it is best to use its value to determine the value, instead of text. In the demo, another one is implemented using the framework, with the same effect.

The above is a short demo to solve the title problem. It is rough. Hope you can have a better way to talk about it. Thank you.

Source code:/files/IT-man/treeviewdemo.rar

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.