Asp.net connects to the database to dynamically generate menu items in the menu

Source: Internet
Author: User

Step 1 create a table

Create Table treetest
(
Id int identity (1, 1) primary key,
Treeid int, -- number structure 1, which indicates the root directory 2, indicating the next level of the root directory, and 3 representing the subdirectories of the root subdirectory .. And so on
Names varchar (200), -- URL-based parameter value
Page varchar (200) -- page
)
-- Add Test Data
Insert into treetest select 1, 'zhang san', 'default. aspx'
Union all select 2, 'lily', 'default1. aspx'
Union all select 2, 'wang wu', 'default2. aspx'
Union all select 2, 'tangsan', 'default3. aspx'
Union all select 3, 'badge', 'default4. aspx'
Union all select 3, 'badge', 'default5. aspx'

Step 2 Write the following code on the page

<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: menu id = "menu1" runat = "server">
</ASP: menu>
</Div>
</Form>
</Body>

Step 3: Write the following code in the background of the page (do not copy the whole because the class name is different)

Using system;
Using system. Data;
Using system. configuration;
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 system. Data. providerbase;
Using system. Data. sqlclient;

Public partial class _ default: system. Web. UI. Page
{
Dataset DS = new dataset ();
Protected void page_load (Object sender, eventargs E)
{

// Menu1.items. Add (I1 );
// I1.childitems. Add (I2 );
// Place user code here to initialize the page
Sqlconnection Cn = new sqlconnection ("Server = data name connection address; user id =; Pwd =; database = tempdb ");

// Initialize the connection string

CN. open ();
// Add command to get data from the database
Sqlcommand sqlcmd = new sqlcommand ();
Sqlcmd. Connection = cn;
Sqlcmd. commandtext = "select * From treetest ";
Sqlcmd. commandtype = commandtype. text;
Sqldataadapter ADP = new sqldataadapter (sqlcmd );
ADP. Fill (DS );

// Call a recursive function to generate a Tree Structure
// TN2 = addtree (0, (treenode) null );
// Treeview1.nodes. Add (TN2 );
Addtree (1, (menuitem) null );
}
Public void addtree (INT parentid, menuitem pnode)
{
String url1;
String url2;
String url3;
String url4;

Menuitem MI = new menuitem ();
// Treenode TN1 = new treenode ();
Dataview dvtree = new dataview (Ds. Tables [0]);
// Filter the parentid to obtain all the current subnodes.
Dvtree. rowfilter = "treeid =" + parentid;
Foreach (datarowview row in dvtree)
{
If (pnode = NULL)
{//'? Add Root Node
Url1 = row ["names"]. tostring ();
Url2 = row ["page"]. tostring () + "? Name = "+ url1;
Mi. Text = url1;
Mi. navigateurl = url2;
Mi. Target = "_ blank ";
// Tn1.text = url1;
// Tn1.navigateurl = url2;
Menu1.items. Add (MI );
// Treeview1.nodes. Add (TN1 );
Addtree (int32.parse (row ["ID"]. tostring () + 1, mi); // recursive again
}
Else
{// Add the subnode of the current node
// Treenode TN2 = new treenode ();

Menuitem m2 = new menuitem ();
Url3 = row ["names"]. tostring ();
Url4 = row ["page"]. tostring () + "? Name = "+ url3;
M2.text = url3;
M2.navigateurl = url4;
M2.target = "_ blank ";
// Tn2.text = url3;
// Tn2.navigateurl = url4;
Pnode. childitems. Add (m2); // modify the location
// Pnode. childnodes. Add (TN2 );
Addtree (int32.parse (row ["ID"]. tostring () + 1, m2); // recursive again
}

}
}
}

 

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.