ASP. NET + SQL server use XML to implement dynamic menus

Source: Internet
Author: User

I. Access to XML data type SQL Server (SQL Server 2005 or 2008)

1. Store XML data into the database

Note:[Spprf] The type in dB is XML.

Sqlparameter sp =   New Sqlparameter ();
Sp. sqldbtype = Sqldbtype. xml;
System. xml. xmlreader XR = System. xml. xmlreader. Create ( @" E: \ A. xml " );
String Strsqlconn =   " ######### " ;
String Strselectsql =   " Insert into [qauser] ([usrid], [uspwd], [spprf]) " ;
Strselectsql + =   " Values ('test', '123', @ XML) " ;
Sqlconnection sqlconn =   New Sqlconnection (strsqlconn );
Sqlcommand sqlcmd =   New Sqlcommand (strselectsql, sqlconn );
Sqlcmd. Parameters. Add ( " @ XML " , Sqldbtype. XML). Value = XR;
Sqlconn. open ();
Sqlcmd. executenonquery ();

2. Retrieve XML

 

2. Bind XMLControls in ASP. NET

Use xmldatasource to dynamically load XML files and form different menus

Xmldatasource settings
< ASP: xmldatasource ID = "Xmlperformance1" Runat = "Server" Datafile = "~ /Test/xmlfile. xml" > </ ASP: xmldatasource >
Navigation File
<? XML version = "1.0" encoding = "UTF-8" ?>
< Menu Title = "Navigation" Description = "" >
< Menuitem URL = "Http://www.baidu.com" Title = "Netease" Description = "" />
< Menuitem URL = "Http://www.google.cn" Title = "Sina" Description = ""   />
< Menuitem URL = "Http://www.163.com" Title = "126" Description = "" />
< Menuitem URL = "Http://www.126.com" Title = "Others" Description = "" />
</ Menu >
ASP: attribute definition in menu
< ASP: Menu ID = "Menu1" Runat = "Server" Orientation = "Horizontal" Performanceid = "Xmlperformance1"
Staticdisplaylevels = "2"   >
< Databindings >
< ASP: menuitembinding Datamember = "Menu" Depth = "0" Textfield = "Title" />
< ASP: menuitembinding Datamember = "Menuitem" Depth = "1" Navigateurlfield = "Url" Textfield = "Title"   />
</ Databindings >
</ ASP: Menu >


Xmldatasource settings
< ASP: xmldatasource ID = "Xmlperformance1" Runat = "Server" Datafile = "~ /Test/xmlfile. xml" > </ ASP: xmldatasource >
Navigation File
<? XML version = "1.0" encoding = "UTF-8" ?>
< Menu Title = "Navigation" Description = "" >
< Menuitem URL = "Http://www.baidu.com" Title = "Netease" Description = "" />
< Menuitem URL = "Http://www.google.cn" Title = "Sina" Description = ""   />
< Menuitem URL = "Http://www.163.com" Title = "126" Description = "" />
< Menuitem URL = "Http://www.126.com" Title = "Others" Description = "" />
</ Menu >
ASP: attribute definition in menu
< ASP: Menu ID = "Menu1" Runat = "Server" Orientation = "Horizontal" Performanceid = "Xmlperformance1"
Staticdisplaylevels = "2"   >
< Databindings >
< ASP: menuitembinding Datamember = "Menu" Depth = "0" Textfield = "Title" />
< ASP: menuitembinding Datamember = "Menuitem" Depth = "1" Navigateurlfield = "Url" Textfield = "Title"   />
</ Databindings >
</ ASP: Menu >

Menuitembinding defines different layers and defines data sources for datamemeber. For example, the level in the first level of datamember is 0, and the level in the second level of data source is menuitem.

Since the first node is the root node, you do not need to set the naviateurlfield attribute.

 

The staticdisplaylevels attribute sets the maximum display level at the beginning of display.

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.