. Net SIMPLE algorithm for unlimited classification (1)

Source: Internet
Author: User
Tags how to design database
In projects, we often need to implement unlimited classification. There are not many. Net examples on the Internet, but many ASP examples. Algorithm Based on the online ASP example, I combined the. NET Treeview control to implement multi-level or even unlimited classification.
For unlimited classification, we must first consider how to design database tables. Program The implementation is very important. My table structure is as follows:

Typeid, which is a type ID. This is an auto-increment field. PID: stores the typeid of the parent node. childrenid: stores the typeid of all its child nodes ', '(no subnode is empty by default); Spath: path. the storage rule is the Spath + 'typeid' of the parent node, separated by commas.
The last two fields are very important. When they write some operations, you will feel very convenient. It is better to delete the operation to find a class. For the convenience of the program algorithm, I set a so-called root node (or root class). In the program, it cannot be deleted, and its PID is set to null (this is very important, the reason is described below ).
The following describes how to implement the function:
1) read operation, because. NET Self-Contained Treeview control, can easily show the form of multi-level classification, so I chose it. There are three methods in total. /**/ /// <Summary> 
///Create a tree menu
/// </Summary> 
Private   Void Buildtree ()
{
System. Data. dataset DS = Cl. getupe ();
DS. relations. Add ( " Noderelation " , DS. Tables [ 0 ]. Columns [ " Typeid " ], DS. Tables [ 0 ]. Columns [ " PID " ]);
Foreach (Datarow dbrow In DS. Tables [ 0 ]. Rows)
{
If (Dbrow. isnull ( " PID " ))
{
String Strtext =   "" ;
String Typeid =   "" ;

If (Dbrow [ " Typename " ] ! =   Null )
{< br> strtext = dbrow [ " typename " ]. tostring ();
typeid = dbrow [ " typeid " ]. tostring ();
}

Treenode newnode = Createnode (strtext, typeid );
Treeview1.nodes. Add (newnode );
Populatesubtree (dbrow, newnode );
}
}
}

/**/ /// <Summary> 
///Use recursive algorithms to create unlimited menu items
/// </Summary> 
Private   Void Populatesubtree (datarow dbrow, treenode node)
{
Foreach (Datarow childrow In Dbrow. getchildrows ( " Noderelation " ))
{
String Strtext =   "" ;
String Typeid =   "" ;

If (Childrow [ " Typename " ] ! =   Null )
{< br> strtext = childrow [ " typename " ]. tostring ();
typeid = childrow [ " typeid " ]. tostring ();
}

Treenode childnode = Createnode (strtext, typeid );
Node. childnodes. Add (childnode );
Populatesubtree (childrow, childnode );
}
}

/**/ /// <Summary> 
///Create menu item
/// </Summary> 
Private Treenode createnode ( String Text, String Typeid)
{
// Javascript: treeview_togglenode (treeview1_data, 0, treeview1n0, '', treeview1n0nodes)
Treenode Node =   New Treenode ();
Node. Text = Text;
Node. Value = Typeid;
Return Node;
}
Protected   Void Treeview1_selectednodechanged ( Object Sender, eventargs E)
{
Typename. Text=Treeview1.selectednode. text;
Hiddenfield1.value=Treeview1.selectednode. value;
}
}

Here, the relations. Add of dataset is used to establish the data parent-child relationship in the table (this method can be found on the Internet for detailed instructions and will not be detailed here .) Note that the PID of the root node must be empty in this method, or an error is reported. In this way, you can call it in page_load. Code As follows: Protected   Void Page_load ( Object Sender, eventargs E)
{< br> If ( ! ispostback)
{< br>
buildtree ();

}
}

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.