Source: Http://hi.baidu.com/kb81/blog/item/c8315f8236e85aa
Recently, a website requires an infinitely hierarchical drop-down list, so the following small works are available.
At first, I thought that the unlimited drop-down list function should be very simple. It is nothing more than a recursion.AlgorithmOkay, ProgramIt is actually a recursive algorithm.
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 + = " -Bytes " ;
This . Getarticlecategory (SDR [ 0 ]. Tostring ());
Toadd = Toadd. substring ( 0 , Toadd. Length - 2 ); // At the beginning, the hierarchy is not displayed :)
}
SDR. Close ();
Conn. Close ();
}
Call Method: InPage_loadCalling
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.
Private string toadd ="Bytes".