The content of this article is only for ASP. NET 2.0 or later is applicable, and the content is very simple. It should not be stored on the homepage, but it is found that there are many developers who do not know it. It is necessary to remind you. So please forgive me if you are familiar with Treeview.
Nowadays, Ajax is popular. Many third-party controls of various types of trees support asynchronous loading.
The members of our project team usually look for some tree controls to use, which brings many risks and problems to the stability of the project.
I suggest you directly use the Treeview control that comes with. net. But can this control be asynchronously loaded?
Let's take a look at the example. Code :
Where Selectaction = "Expand" Populateondemand = "True"These two attributes are critical. In the background code, you can also see that you need to set relevant attributes for nodes with subnodes. < ASP: Treeview ID = "Treeview1" Runat = "Server" Enableclientscript = "True" Ontreenodepopulate = "Populatenode" Imageset = "Arrows" Expanddepth = "0" >
< Nodes >
< ASP: treenode Text = "Department tree" Value = "Department tree" Selectaction = "Expand" Populateondemand = "True" />
</ Nodes >
</ ASP: Treeview >
Background code:
Note that populatenode is written in this code because I connect a department at each level to different database tables. If the Department data at each level is in the same table, the code is simpler.
Topsmart. utilclass. Data. idatabase: the class that encapsulates the database by myself. You can replace and use the frequently used database class. Public Void Populatenode (Object sender, treenodeeventargs E)
{
Populateme (E. node, E. node. Depth );
}
Public Void Populateme (treenode node, Int Depth)
{
Topsmart. utilclass. Data. idatabase DB = Topsmart. utilclass. Data. dataaccess. databasecreate ( " HR " );
Datatable DT;
If (Depth = 0 )
DT = DB. getdatatablefromsql ( " Select T. onelevelname as name, T. onelevelno as no from hrt_onelevel_dic t where T. Valid = 1 " );
Else If (Depth = 1 )
DT = DB. getdatatablefromsql ( " Select T. twolevelname as name, T. twolevelno as no from hrt_twolevel_dic t where T. Valid = 1 and T. onelevelno =' " + Node. Value + " ' " );
Else
DT = DB. getdatatablefromsql ( " Select T. deptname as name, T. deptno as no from hrt_dept_dic t where T. Valid = 1 and T. twolevelno =' " + Node. Value + " ' " );
If (Dt. Rows. Count > 0 )
{
Foreach (Datarow row In DT. Rows)
{
Treenode newnode = New Treenode (row [ " Name " ]. Tostring (), row [ " No " ]. Tostring ());
Newnode. populateondemand = (Depth = 2 ) ? False : True ;
Newnode. selectaction = Treenodeselectaction. Expand;
Node. childnodes. Add (newnode );
}
}
}
Newnode. populateondemand=(Depth= 2)? False:TrueWhen the value is 2, there is no lower sub-door in my system. You can use it flexibly.
The code is complete. Let's see, isn't it easy?
In addition, it is easy to control which node can be asynchronously loaded and which node does not need to be used. No script or web service.
For official documentation, refer to msdn: Address:
MS-help: // Ms. VSCC. v80/ms. msdn. v80/ms. netdevfx. v1_chs/cpref16/html/p_system_web_ui_webcontrols_treenode_populateondeman.htm