I haven't found an example on the Internet for a long time. I can't help it. I can't steal it if I want to be lazy. I 'd like to write it honestly. Not much nonsense. Please refer to my detailed method below.
1. The database must support unlimited classification. You can dynamically add their classification. below is the structure of the database:
The database mainly has three fields, which is the most basic. The first is the primary key, the second is the category name, and the third is the parent directory ID.
2. Finished Products: (do you most care about this? Let's see what it looks like.ProgramWhy)
Look !! This is what it looks like. This hierarchy is the result of the above data. How is it? Okay...
3. Finally, it is the provider. The program is actually a recursion. Algorithm . CopyCode The Code is as follows: Private void getarticlecategory (string PID)
{
Sqlconnection conn = new sqlconnection ("Server =.; database = test; uid = sa; Pwd = ;");
String SQL = "select articlesgroup_id, groupname from articlesgroup where articlesgroup_parent_id = @ PID order by groupname ";
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Sqlparameter pid = new sqlparameter ("@ PID", sqldbtype. INT );
PID. value = PID;
Cmd. Parameters. Add (PID );
Conn. open ();
Sqldatareader SDR = cmd. executereader ();
While (SDR. Read ())
{
This. dropdownlist1.items. Add (New listitem (toadd + "" + SDR [1]. tostring (), SDR [0]. tostring ()));
Toadd + = "-Example ";
This. getarticlecategory (SDR [0]. tostring ());
Toadd = toadd. substring (0, toadd. Length-2); // It is not properly processed at the beginning, and the hierarchy cannot be displayed :)
}
SDR. Close ();
Conn. Close ();
}
Call method: Call in page_loadCopy codeThe Code is as follows: protected void page_load (Object sender, eventargs E)
{
This. Keyword. Attributes. Add ("onfocus", "If (this. value = 'key key key') {This. value = ''};");
If (! Page. ispostback)
{
This. getarticlecategory ("0 ");
}
This. dropdownlist1.items. insert (0, new listitem ("search all", "all "));
}
Define a variable in the class.Copy codeThe Code is as follows: Private string toadd = "success ";