This sample shows how to bind the ASPxMenu to data stored in a database.
Question Comments
Added By: ATIG at: 5/21/2013 12:44:50 AM
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BuildMenu(ASPxMenu1);
BuildMenu(ASPxMenu3);
}
}
protected void BuildMenu(ASPxMenu menu)
{
String sql = "select * from menus where Menu_Level = 0 and Menu_Status = 1";
cMenus oMenus = new cMenus();
DataSet ds = oMenus.GetRecordsToDs(sql);
Int32 menu_id;
foreach (DataRow row in ds.Tables[0].Rows)
{
menu_id = Convert.ToInt32(row["Menu_Id"]);
DevExpress.Web.ASPxMenu.MenuItem item = menu.RootItem.Items.Add();
item.Name = Convert.ToString(row["Menu_Name"]);
item.Text = Convert.ToString(row["Menu_Name"]);
DataSet iDs = GetGroupItems(menu_id);
for (int i = 0; i < iDs.Tables[0].Rows.Count; i++)
{
DevExpress.Web.ASPxMenu.MenuItem ChildItem = new DevExpress.Web.ASPxMenu.MenuItem();
ChildItem.Name = iDs.Tables[0].Rows[i]["Menu_Name"].ToString();
ChildItem.Text = iDs.Tables[0].Rows[i]["Menu_Name"].ToString();
ChildItem.NavigateUrl = iDs.Tables[0].Rows[i]["Menu_SeoUrl"].ToString();
item.Items.Add(ChildItem);
}
}
}
private DataSet GetGroupItems(Int32 menu_id)
{
cMenus oMenus = new cMenus();
String sql = "select * from menus where Menu_Level = 1 and Menu_Sub = " + menu_id + " order by Menu_Sort";
DataSet ds = oMenus.GetRecordsToDs(sql);
return ds;
}
public DataSet GetRecordsToDs(String sqlStr)
{
IDbConnection Conn = (IDbConnection)new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
Conn.Open();
IDbCommand dbCommand = new SqlCommand();
dbCommand.CommandText = sqlStr;
dbCommand.Connection = Conn;
IDbDataAdapter dataAdapter = (IDbDataAdapter)new SqlDataAdapter();
dataAdapter.SelectCommand = dbCommand;
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
Conn.Close();
return ds;
}
I had a question on this.
Could someone explain how the child items are getting added to the menu?
For the root items menu.Items.Add(item) makes sense to me.
I understand menuItems.Add(itemID, item) does this somehow but how does that method know which menu to add the items too?
Thanks
Nic
Hello Nic,
The menuItems dictionary contains MenuItem objects. Since root items are already added to the menu control, and the subitem added to the root item (that we obtain from the dictionary by key) automatically becomes a part of the whole hierarchy.
Added By: Nic Carreira at: 10/24/2013 10:21:53 AMOk, this makes sense now.
Thanks
Added By: Dale Jensen Lodge at: 5/10/2016 11:53:59 PM Hi it would be good to get sight of the underlying table that this code is referencing so we can see exactly what it's looking at. ThanksAdded By: Artem (DevExpress Support) at: 5/11/2016 12:34:13 AM
Hi Dale,
I see you asked this question in the https://www.devexpress.com/Support/Center/Example/Details/E49 thread. Please refer to it for further correspondence.