Use ASP. NET menu and SQL database to create unlimited menus

Source: Internet
Author: User
ArticleDirectory
    • Source: Internet
Source: Internet
Using System;
Using System. Data;
Using System. configuration;
Using System. collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Using System. Data. sqlclient;
Using WebTools;
PublicPartialClass Default4: system. Web. UI. Page
{
Public Int Parentid;
PublicTools connectionstr1= New Tools ();
Protected VoidPage_load (Object Sender, eventargs E)
{
StringConnectionstr= Connectionstr1.connstr. connectionstring;
Sqlconnection sconnstr= New Sqlconnection (connectionstr );
//Set database operation commands
Sconnstr. open ();
Sqlcommand scmd= NewSqlcommand ("Select * From menutable where parentid = 0" , Sconnstr );
Sqldatareader ODR= Scmd. executereader ();
While (ODR. Read ())
{
Menuitem menunode= New Menuitem ();
Menunode. Text=ODR ["Menuname" ]. Tostring ();
Menunode. Value=ODR ["ID" ]. Tostring ();
Menunode. Enabled= True ;
Menunode. Target=ODR ["Target" ]. Tostring ();
Menunode. navigateurl=ODR ["URL" ]. Tostring ();
Menu1.items. Add (menunode );
Parentid=Convert. toint16 (ODR ["ID" ]. Tostring ());
Addchildmenu1 (menunode, menunode. value );
}
//Close database connection
Scmd. Connection. Close ();
}
Protected VoidAddchildmenu1 (menuitem pnode,String PID)
{
StringConnectionstr= Connectionstr1.connstr. connectionstring;
Sqlconnection sconnstr1= New Sqlconnection (connectionstr );
//Set database operation commands
Sconnstr1.open ();
Sqlcommand scmd1= NewSqlcommand ("Select * From menutable where parentid =" +Parentid+ "" , Sconnstr1 );
Sqldatareader ODR= Scmd1.executereader ();
While (ODR. Read ())
{
Menuitem menunode= New Menuitem ();
Menunode. Text=ODR ["Menuname" ]. Tostring ();
Menunode. Value=ODR ["ID" ]. Tostring ();
Menunode. Enabled= True ;
Menunode. Target=ODR ["Target" ]. Tostring ();
Menunode. navigateurl=ODR ["URL" ]. Tostring ();
Pnode. childitems. Add (menunode );
Parentid=Convert. toint16 (ODR ["ID" ]. Tostring ());
Addchildmenu2 (menunode, menunode. value );
}
//Close database connection
Scmd1.connection. Close ();
}
Protected VoidAddchildmenu2 (menuitem pnode,String PID)
{
StringConnectionstr= Connectionstr1.connstr. connectionstring;
Sqlconnection sconnstr1= New Sqlconnection (connectionstr );
//Set database operation commands
Sconnstr1.open ();
Sqlcommand scmd1= NewSqlcommand ("Select * From menutable where parentid =" +Parentid+ "" , Sconnstr1 );
Sqldatareader ODR= Scmd1.executereader ();
While (ODR. Read ())
{
Menuitem menunode= New Menuitem ();
Menunode. Text=ODR ["Menuname" ]. Tostring ();
Menunode. Value=ODR ["ID" ]. Tostring ();
Menunode. Enabled= True ;
Menunode. Target=ODR ["Target" ]. Tostring ();
Menunode. navigateurl=ODR ["URL" ]. Tostring ();
Pnode. childitems. Add (menunode );
Parentid=Convert. toint16 (ODR ["ID" ]. Tostring ());
Addchildmenu1 (menunode, menunode. value );
}
//Close database connection
Scmd1.connection. Close ();
Related Article

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.