When the Treeview control in ASP. NET applications needs to fill in the treenode data, the node expansion problem occurs.

Source: Internet
Author: User

When using the Treeview Server Control, the static definition of the data structure is sometimes not feasible, or the data may depend on the information collected during the runtime. To dynamically display data,TreeviewControls Support Dynamic node filling. SetTreeviewWhen the control is configured to be filled, the control triggers an event when the user expands the node. Event ProcessingProgramSearch the corresponding data and fill it with the node you clicked. To fill in dataTreenodeObject. Set the populateondemand attribute of the nodeTrueAnd create a treenodepopulate event handlerTreenodeObject. (Derived from M $ DN)

Example:

<Asp: Treeview id = "trvtreemenu" runat = "server" autogeneratedatabindings = "false" expanddepth = "0" populateondemand = "true" showexpandcollapse = "true"
Width = "195px"Ontreenodepopulate = "treeviewappstreenodepopulate"Expandimageurl = "img/selectchildnode.gif" font-names = "" font-size = "10pt" Height = "100%" showlines = "true" forecolor = "black">
<Nodes>

<Asp: treenode value = "1" text = "Root Node"Populateondemand = "true"Navigateurl = "#"> </ASP: treenode>

</Nodes>

</ASP: Treeview>

At the backendCodeYou can implement the treeviewinclutreenodepopulate method to dynamically fill the treenode node.

Public void treeviewinclutreenodepopulate (Object sender, treenodeeventargs E)
{
If (iscallback)
If (E. node. childnodes. Count = 0)
{
Loadchildnode (E. node );
}
}

However, you will find that because the node is dynamically filled, the entire Treeview is always in the shrinking State when the page is just opened, whether or not you have set

Showexpandcollapse, you will also find that the treeviewmediatreenodepopulate function will be triggered only by clicking the mouse to expand the tree.

In this way, you want to make the tree partially expand after the page is loaded, and you want to usePopulateondemandOnly manually attach nodes to be expanded.Populateondemand = "true"Method

The original code is slightly modified.

<Asp: Treeview id = "trvtreemenu" runat = "server" autogeneratedatabindings = "false" expanddepth = "0" populateondemand = "true" showexpandcollapse = "true"
Width = "195px"Ontreenodepopulate = "treeviewappstreenodepopulate"Expandimageurl = "img/selectchildnode.gif" font-names = "" font-size = "10pt" Height = "100%" showlines = "true" forecolor = "black">
<Nodes>

<Asp: treenode value = "1" text = "Root Node"Populateondemand = "false"Navigateurl = "#"> </ASP: treenode>

</Nodes>

</ASP: Treeview>

Add

Loadchildnode (trvtreemenu. nodes [0 ]);

When a node is added to loadchildnode, subsequent nodes are opened as needed:

Child. populateondemand = true;

Summary:

In the Treeview control, when populateondemand is used for on-demand loading, subnodes cannot exist under it, that is, initialization cannot be performed manually. To manually initialize populateondemand, set it to false.

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.