asp.net-based webmenu data manipulation 4

Source: Internet
Author: User

The program code is as follows:

Using System;

Using System.Collections;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Web;

Using System.Web.SessionState;

Using System.Web.UI;

Using System.Web.UI.WebControls;

Using System.Web.UI.HtmlControls;

Using System.Data.OleDb;

Namespace WebApplication6

{

<summary>

Summary description of the WebForm1.

</summary>

public class WebForm1:System.Web.UI.Page

{

protected Coalesys.WebMenu.WebMenu Csnetmenu;

private void Page_Load (object sender, System.EventArgs e)

{

Place user code here to initialize page

csNetMenu.MenuBar.AbsoluteDockEnabled = false;

csNetMenu.MenuBar.AbsoluteDragEnabled = false;

CsNetMenu.MenuBar.BackgroundColor = "";

CsNetMenu.MenuBar.OuterHighlightColor = "#666666";

CsNetMenu.MenuBar.OuterShadowColor = "#666666";

CsNetMenu.MenuBar.InnerShadowColor = "#F9F8F7";

CsNetMenu.MenuBar.HoverColor = "#dfdfdf";

CsNetMenu.MenuBar.SelectedColor = "#B6BDD2";

CsNetMenu.MenuBar.SelectedTextColor = "#000000";

Csnetmenu.backgroundcolor = "";

Csnetmenu.selectedcolor = "#B6BDD2";

Csnetmenu.outerhighlightcolor = "#c0c0c0";

Csnetmenu.outershadowcolor = "#c0c0c0";

Csnetmenu.innershadowcolor = "#808080";

Csnetmenu.popupicon = "./images/arrow-black.gif";

Csnetmenu.selectedpopupicon = "./images/arrow-white.gif";

Csnetmenu.clearpixelimage = "./images/clearpixel.gif";

Populate Webmenu

Loadwebmenudata (Csnetmenu);

}

//=============================================================================

Loadwebmenudata-load Webmenu from Database

//

Input

Cswebmenu-[in] Coalesys.WebMenu.WebMenu object

//

Output

None

//

public void Loadwebmenudata (Coalesys.WebMenu.WebMenu cswebmenu)

{

Coalesys.WebMenu.Group Csmenugroup;

Database info

String dbconnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=";

String dbpathstring = Server.MapPath ("./selfreferencedtable.mdb");

String dbsqlstring = "SELECT * from Nodes order by ID";

Initiate OLE DB interface

OleDbConnection dbconn = new OleDbConnection (dbconnstring + dbpathstring);

OleDbCommand Dbcomm = new OleDbCommand (dbsqlstring, dbconn);

OleDbDataAdapter dbadapter = new OleDbDataAdapter ();

Dbconn.open ();

Fill an ado.net DataSet

DataSet ds = new DataSet ();

Dbadapter.selectcommand = Dbcomm;

Dbadapter.fill (ds, "MenuItems");

Dbconn.close ();

Create the data relation between the ID and parent_id columns of the MenuItems table.

(This is the key to hierarchical navigating in a self-referencing table).

DataRelation dr = ds. Relations.Add ("Menuitemhierarchy",

Ds. tables["MenuItems"]. columns["ID"],

Ds. tables["MenuItems"]. columns["parent_id"]);

Start Top-down Navigation of the MenuItem rows.

foreach (DataRow dbmenuitem in DS. tables["MenuItems"]. Rows)

{

If the parent_id colum is null, then it is a root menu item.

if (Dbmenuitem.isnull ("parent_id"))

{

Create a menu group for the root menu item

Csmenugroup = CsWebMenu.Groups.Add ();

Csmenugroup.caption = dbmenuitem["Caption"]. ToString ();

Execute the recursive function to populate all it ' s children.

Addmenuitems (Dbmenuitem.getchildrows (DR), Dr, Csmenugroup);

}

}

}

//=============================================================================

addmenuitems-recursive function to populate hierarchical Menu Items

From data rows that have parent/child relationships.

//

Input

DataRows-[in] child Rows

Datarel-[in] Data relation

Webmenugroup-[in] Webmenu Group

//

Output

None

//

public void Addmenuitems (datarow[] DataRows, DataRelation Datarel, Coalesys.WebMenu.Group webmenugroup)

{

Coalesys.WebMenu.Item Csmenuitem;

Coalesys.WebMenu.Group Csnestedmenugroup;

Datarow[] Drchildren;

foreach (DataRow dbmenuitem in DataRows)

{

Csmenuitem = WebMenuGroup.Items.Add ();

Csmenuitem.caption = dbmenuitem["Caption"]. ToString ();

Csmenuitem.url = dbmenuitem["URL"]. ToString ();

if (dbmenuitem["Enable"]. ToString () = = "True")

{

Csmenuitem.enabled=true;

}

Else

{

Csmenuitem.enabled=false;

}

Check if this Item has children of it ' s own

Drchildren = Dbmenuitem.getchildrows (Datarel);

If so, create a group for the children and reenter this function.

if (Drchildren.length > 0)

{

Csnestedmenugroup = Csmenuitem.addgroup ();

Addmenuitems (Drchildren, Datarel, Csnestedmenugroup);

}

}

}

}


The effect chart is as follows:




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.