Example E49
Visible to All Users

Menu for ASP.NET Web Forms - How to bind a control to a database

This sample shows how to bind the ASPxMenu to data stored in a database.

Files to Review

Example Code

WebSite/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ASPxperience_Menu_BuildMenuFromDB_BuildMenuFromDB" %> <%@ Register Assembly="DevExpress.Web.v13.1" Namespace="DevExpress.Web" TagPrefix="dxwm" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Binding a menu to a database</title> </head> <body> <form id="form1" runat="server"> <div> <dxwm:ASPxMenu ID="ASPxMenu1" runat="server"></dxwm:ASPxMenu> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/MenuDB.mdb" SelectCommand="SELECT * FROM [MenuData]"></asp:AccessDataSource> </div> </form> </body> </html>
WebSite/Default.aspx.cs(vb)
C#
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.Collections.Generic; public partial class ASPxperience_Menu_BuildMenuFromDB_BuildMenuFromDB : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { BuildMenu(ASPxMenu1, AccessDataSource1); } protected void BuildMenu(DevExpress.Web.ASPxMenu menu, SqlDataSource dataSource) { // Get DataView DataSourceSelectArguments arg = new DataSourceSelectArguments(); DataView dataView = dataSource.Select(arg) as DataView; dataView.Sort = "ParentID"; // Build Menu Items Dictionary<string, DevExpress.Web.MenuItem> menuItems = new Dictionary<string, DevExpress.Web.MenuItem>(); for (int i = 0; i < dataView.Count; i++) { DataRow row = dataView[i].Row; DevExpress.Web.MenuItem item = CreateMenuItem(row); string itemID = row["ID"].ToString(); string parentID = row["ParentID"].ToString(); if (menuItems.ContainsKey(parentID)) menuItems[parentID].Items.Add(item); else { if (parentID == "0") // It's Root Item menu.Items.Add(item); } menuItems.Add(itemID, item); } } private DevExpress.Web.MenuItem CreateMenuItem(DataRow row) { DevExpress.Web.MenuItem ret = new DevExpress.Web.MenuItem(); ret.Text = row["Text"].ToString(); ret.NavigateUrl = row["NavigateUrl"].ToString(); return ret; } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.