Use the custom site map provider to control ASP. NET menus

Source: Internet
Author: User

The site map provided by ASP. NET provides great convenience for creating navigation bars and menus. In addition, we can build our own site map provider to make our menu items controllable.

 

First, let's take a look at the relationship between navigation controls such as menu and site maps. In ASP
The Web. sitemap created in the. NET Project
It is actually an XML file that complies with certain specifications. We call it a site map file. Dot Net
Defines the site map provider for us. The task completed by the site map provider is to read the data we need as a navigation. For example, ASP. NET
The default site map provider is xmlsitemapprovider (which is actually a class). In general, menu and other Web navigation controls read data from the Web. sitemap file through this class.

 

Dot Net
There is a default site map provider, and it also allows us to create our own site map provider (class), that is, to provide a class to read data from other data sources. In most cases, our data is stored in the database, not just fixed in the web. sitemap file. In this case, the website map provider can inherit the staticsitemapprovider class. Three methods to implement this class must be overwritten, including initialize, getrootnodecore, and buildsitemap.

 

After completing our site map provider, we also need to tell ASP Dot Net
The default provider is the class we created. This needs to be set in the web. config configuration file. The Code is as follows:

<Sitemap>


<Providers>


<Add
Name = "mysitemapprovider"
Type = "siteproviderexe"
Conn = "Data Source = MICROSO-1PGF29S/sqlexpress; initial catalog = demo; user id = sa; Password = 123;"/>


</Providers>


</Sitemap>

Note that this part of the code is set in the <system. Web> label in the configuration file. Type)It is the type of the class of our site map provider, and the name is random, followed by a string that connects to the database.

After this is done, we also need to add a node in the web. sitemap file: <sitemapnode
Provider = "mysitemapprovider"/>. Note that this is the name.
.

Save it in the databaseStorageOkay, so we can use our files. But now we haven't provided the implementation code for our own site map for the program, as shown below:

 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; // introduce the required namespace using system. data. sqlclient; using system. collections. specialized; using system. collections; using system. security. permissions;/** // <summary> // Summary of sqlsitemap // /</Summary> [aspnethostingpermission (securityaction. demand, Level = aspnethostingpermissionlevel. minimal)] // permission description public class siteproviderexe: staticsitemapprovider {private sitemapnode smnode = NULL; private sitemapnode rootnode = NULL; private sqlconnection conn = NULL; private string connstring = "smcstring "; // declare the node, database connection, and connection string variables public sqlsitemap () {} private bool initialized = false; Public Virtual bool isinitialized {// rewrite the isinitialized attribute of the base class. After the base class initialize is called, execute the initialization get {return initialized;} protected override sitemapnode getrootnodecore () in this method () {// override the base class getrootnodecore method to return the rootnode property return buildsitemap ();} public override void initialize (string name, namevaluecollection attributes) {// override the base class initialize method, initialize the data connection. If successful, set initialized to true if (isinitialized) {return;} base. in Itialize (name, attributes); // call the initialize method of the base class to initialize string sqlconnstring = attributes [connstring]; If (sqlconnstring = NULL) {// check whether the database connection has throw new exception ("data connection does not exist");} else {// initialize the connection conn = new sqlconnection (sqlconnstring);} initialized = true ;} protected override void clear () {lock (this) {If (rootnode = NULL) base. clear () ;}} public override sitemapnode buildsitemap () {lock (this ){// Lock if (! Isinitialized) {// determines whether throw new exception ("data is not initialized");} else {If (rootnode = NULL) {clear (); int rootnodeid =-1; if (Conn. state = connectionstate. closed) {// if the current connection is closed, open it Conn. open () ;}// loop sqlcommand cmd = new sqlcommand ("select RID, URL, title, DISP from rootsitemap where Rn = 0", Conn); sqldatareader SDR = cmd. executereader (); If (SDR. hasrows) {SDR. read (); smnode = new sitemapnode (this, SDR. getstring (0), SDR. getstring (1), SDR. getstring (2), SDR. getstring (3);} SDR. close (); // enter the rootnode node sqldataadapter rootsda = new sqldataadapter ("select RID, URL, title, DISP from rootsitemap where Rn = 1", Conn ); dataset DS = new dataset (); rootsda. fill (DS, "root"); dataview RDV = Ds. tables ["root"]. defaultview; If (RDV. count> 0) {foreach (datarowview DRV in RDV) {rootnodeid = convert. toint32 (DRV ["RID"]); rootnode = new sitemapnode (this, DRV ["RID"]. tostring (), DRV ["url"]. tostring (), DRV ["title"]. tostring (), DRV ["disp"]. tostring (); // enter the subnode to query sqlcommand childcmd = new sqlcommand ("select NID, URL, title, DISP from sitemap where node =" + rootnodeid + "", conn); sqldatareader childread = childcmd. executereader (); If (childread. hasrows) {sitemapnode childnode = NULL; while (childread. read () {childnode = new sitemapnode (this, childread. getstring (0 ). tostring (), childread. getstring (1), childread. getstring (2), childread. getstring (3); addnode (childnode, rootnode) ;}} childread. close (); addnode (rootnode, smnode);} Conn. close () ;}else {return NULL ;}} return smnode ;}}}

 

The above site map provider needs to read data from the database. It uses two tables, as shown below:

 

This is generally the case. The focus of the implementation is to understand that the function of the Site Map provider is to read the data that needs to be displayed and connected in Web navigation controls such as menu from the data source. In this example, SQL Server is used as the data source. However, the data source of xmlsitemapprovider is a file of the sitemap type. To make it clear, we also need to take a look at staticsitemapprovider, xmlsitemapprovider, and sitemapnode in the systemweb namespace.
And the use of dataview in the system. Data namespace.

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.