Create a method of GetMenuData to read data of menu from database to dataset.
DataSet GetMenuData()
{
SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter dadCats = new SqlDataAdapter("SELECT * FROM Categories", con);
SqlDataAdapter dadProducts = new SqlDataAdapter("SELECT * FROM Products", con);
DataSet dst = new DataSet();
dadCats.Fill(dst, "Categories");
dadProducts.Fill(dst, "Products");
dst.Relations.Add("Children",
dst.Tables["Categories"].Columns["CategoryID"],
dst.Tables["Products"].Columns["CategoryID"]);
return dst;
}
Fill data to menu control
public void PopulateMenu()
{
DataSet dst = GetMenuData();
foreach (DataRow masterRow in dst.Tables["Categories"].Rows)
{
MenuItem masterItem = new MenuItem((string)masterRow["CategoryName"]);
Menu1.Items.Add(masterItem);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
{
MenuItem childItem = new MenuItem((string)childRow["ProductName"]);
masterItem.ChildItems.Add(childItem);
}
}
}
Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
PopulateMenu();
}
Get an alternative here with source code:
http://www.codeguru.com/csharp/csharp/cs_misc/designtechniques/article.php/c15661[
^]