The following describes two unlimited menu display methods used in development. The first is to place the menu in the dropdownlist control. The Code is as follows:
- Private void maketree ()
- {
- Int mainid = 0;
- Datatable dt = gettable (mainid );
- Try
- {
- If (DT! = NULL)
- {
- For (INT I = 0; I <DT. Rows. Count; I ++)
- {
- String T = "|-" + dt. Rows [I] ["dept_name"]. tostring (). Trim ();
- String v = DT. Rows [I] ["ID"]. tostring ();
- Listitem li = new listitem (T, V );
- This. dropdownlist1.items. Add (LI );
- Addsub (Int. parse (Dt. Rows [I] ["ID"]. tostring (), 0 );
- }
- }
- }
- Catch (exception ex)
- {
- Response. Write (ex. Message );
- }
- }
- Private void addsub (int pid, int level)
- {
- Datatable dt = gettable (PID );
- Try
- {
- If (DT! = NULL)
- {
- For (INT I = 0; I <DT. Rows. Count; I ++)
- {
- String blank = "| ";
- For (int m = 1; m <= level; m ++)
- {
- Blank = blank + "";
- }
- String T = blank + "|-" + dt. Rows [I] ["dept_name"]. tostring ();
- String v = DT. Rows [I] ["ID"]. tostring ();
- Listitem li = new listitem (T, V );
- This. dropdownlist1.items. Add (LI );
- If (Dt. Rows. Count> 0)
- {
- Addsub (Int. parse (Dt. Rows [I] ["ID"]. tostring (), level + 1 );
- }
- Else
- {
- Addsub (Int. parse (Dt. Rows [I] ["parentid"]. tostring (), level + 1 );
- }
- }
- }
- }
- Catch (exception ex)
- {
- Response. Write (ex. Message );
- }
- }
The second method is to help set it to the Treeview control. The Code is as follows:
- Private void addtreenodes (int pid, treenode node)
- {
- Datatable dt = gettable (PID );
- Try
- {
- If (DT! = NULL)
- {
- For (INT I = 0; I <DT. Rows. Count; I ++)
- {
- Treenode nodes = new treenode ();
- Nodes. Text = DT. Rows [I] ["dept_name"]. tostring ();
- Nodes. value = DT. Rows [I] ["ID"]. tostring ();
- Node. childnodes. Add (nodes );
- If (Dt. Rows. Count> 0)
- {
- Addtreenodes (Int. parse (Dt. Rows [I] ["ID"]. tostring (), nodes );
- }
- Else
- {
- Addtreenodes (Int. parse (Dt. Rows [I] ["parentid"]. tostring (), nodes );
- }
- }
- }
- }
- Catch (exception ex)
- {
- Response. Write (ex. Message );
- }
- }
- Protected void maketree ()
- {
- Int mainid = 0;
- Datatable dt = gettable (mainid );
- Try
- {
- If (DT! = NULL)
- {
- For (INT I = 0; I <DT. Rows. Count; I ++)
- {
- Treenode nodes = new treenode ();
- Nodes. Text = DT. Rows [I] ["dept_name"]. tostring ();
- Nodes. value = DT. Rows [I] ["ID"]. tostring ();
- This. treeview1.nodes. Add (nodes );
- Addtreenodes (Int. parse (Dt. Rows [I] ["ID"]. tostring (), nodes );
- }
- }
- }
- Catch (exception ex)
- {
- Response. Write (ex. Message );
- }
- }
- Private datatable gettable (INT refID)
- {
- Datatable dt = new datatable ();
- Bussinessdataaccess DBA = new bussinessdataaccess ();
- Try
- {
- Dt = DBA. selectdatabysql ("select * From user_address_deptinfo where parentid = '" + refID + "'", "tablename ");
- }
- Catch (exception ex)
- {
- Response. Write (ex. Message );
- }
- Return DT;
- }
You can also bind it to other controls, such as menu and sitemap controls. The database structure is as follows.