.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Working with Menu in ASP.NET

Posted By:Athira Sreejith       Posted Date: February 22, 2009    Points: 5    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Working with Menu control in ASP.NET. Below is the sample code to do that.

private void fillMenu()

{

string strConnectionString = ConfigurationManager.ConnectionStrings["aaa"].ConnectionString;

MenuItem mainMenuItem1 = new MenuItem();

mainMenuItem1.Text = "Home";

mainMenuItem1.NavigateUrl = "Home.aspx";

mainMenuItem1.SeparatorImageUrl = "~/images/menu_line.gif";

MainMenu.Items.Add(mainMenuItem1);

 

MenuItem mainMenuItem2 = new MenuItem();

mainMenuItem2.Text = "About Us";

mainMenuItem2.NavigateUrl = "NewAboutUs.aspx";

mainMenuItem2.SeparatorImageUrl = "~/images/menu_line.gif";

MainMenu.Items.Add(mainMenuItem2);

MenuItem mainMenuItem3 = new MenuItem();

mainMenuItem3.Text = " Products & Services ";

mainMenuItem3.SeparatorImageUrl = "~/images/menu_line.gif";

 

 

//needed for checking its products

string strMenuItemProductsID = "", strSecMenuItemProductsID = "";

string strParentItemProduct = "", strSecParentItemProduct = "";

string strChildItemProduct = "", strSecChildItemProduct = "";

string strChildItem1Product = "", strSecChildItem1Product = "";

 

using(SqlConnection ConparentDetails = new SqlConnection(strConnectionString))

{

ConparentDetails.Open();

SqlCommand CmdparentDetails = new SqlCommand("select Name,ID,Cat_Level from Category where isnull(ParentID,'') = '' order by Priority", ConparentDetails);

using (SqlDataReader drparentDetails = CmdparentDetails.ExecuteReader())

{

while (drparentDetails.Read())

{

strMenuItemProductsID = drparentDetails["ID"].ToString();

#region first Node

MenuItem ParentItem = new MenuItem();

ParentItem.Text = "" + drparentDetails["Name"].ToString();

//ParentItem.ToolTip = drparentDetails["Name"].ToString();

ParentItem.Value = drparentDetails["ID"].ToString();

ParentItem.NavigateUrl = "Products.aspx?MenuID=" + drparentDetails["ID"].ToString();

ParentItem.Selectable = true;

//int OnceParentItemProduct = 0;

//SQLHelper childDetails1 = new SQLHelper();

//SqlDataReader drchildDetails1 = childDetails1.ExecuteReader("select Name,ID,Cat_Level from Category where ParentID='" + drparentDetails["ID"].ToString() + "'");

using (SqlConnection ConchildDetails1 = new SqlConnection(strConnectionString))

{

SqlCommand CmdchildDetails1 = new SqlCommand("select Name,ID,Cat_Level from Category where ParentID='" + drparentDetails["ID"].ToString() + "' order by Priority", ConchildDetails1);

ConchildDetails1.Open();

using (SqlDataReader drchildDetails1 = CmdchildDetails1.ExecuteReader())

{

while (drchildDetails1.Read())

{

strParentItemProduct = drchildDetails1["ID"].ToString();

#region Second Node

MenuItem ChildItem = new MenuItem();

ChildItem.Text = "" + drchildDetails1["Name"].ToString();

//ChildItem.ToolTip = drchildDetails1["Name"].ToString();

ChildItem.Value = drchildDetails1["ID"].ToString();

ChildItem.NavigateUrl = "Products.aspx?MenuID=" + drchildDetails1["ID"].ToString();

ChildItem.Selectable = true;

//int OnceChildItemProduct = 0;

//SQLHelper childDetails2 = new SQLHelper();

//SqlDataReader drchildDetails2 = childDetails2.ExecuteReader("select Name,ID,Cat_Level from Category where ParentID='" + drchildDetails1["ID"].ToString() + "'");

using (SqlConnection ConchildDetails2 = new SqlConnection(strConnectionString))

{

SqlCommand CmdchildDetails2 = new SqlCommand("select Name,ID,Cat_Level from Category where ParentID='" + drchildDetails1["ID"].ToString() + "' order by Priority", ConchildDetails2);

ConchildDetails2.Open();

using (SqlDataReader drchildDetails2 = CmdchildDetails2.ExecuteReader())

{

while (drchildDetails2.Read())

{

strChildItemProduct = drchildDetails2["ID"].ToString();

#region Third Node

MenuItem ChildItem1 = new MenuItem();

ChildItem1.Text = "" + drchildDetails2["Name"].ToString();

//ChildItem1.ToolTip = drchildDetails2["Name"].ToString();

ChildItem1.Value = drchildDetails2["ID"].ToString();

ChildItem1.NavigateUrl = "Products.aspx?MenuID=" + drchildDetails2["ID"].ToString();

ChildItem1.Selectable = true;

//int OnceChildItem1Product = 0;

//SQLHelper childDetails3 = new SQLHelper();

//SqlDataReader drchildDetails3 = childDetails3.ExecuteReader("select Name,ID,Cat_Level from Category where ParentID='" + drchildDetails2["ID"].ToString() + "'");

using (SqlConnection ConchildDetails3 = new SqlConnection(strConnectionString))

{

SqlCommand CmdchildDetails3 = new SqlCommand("select Name,ID,Cat_Level from Category where ParentID='" + drchildDetails2["ID"].ToString() + "' order by Priority", ConchildDetails3);

ConchildDetails3.Open();

using (SqlDataReader drchildDetails3 = CmdchildDetails3.ExecuteReader())

{

while (drchildDetails3.Read())

{

strChildItem1Product = drchildDetails3["ID"].ToString();

MenuItem ChildItem2 = new MenuItem();

ChildItem2.Text = "" + drchildDetails3["Name"].ToString();

//ChildItem2.ToolTip = drchildDetails3["Name"].ToString();

ChildItem2.Value = drchildDetails3["ID"].ToString();

ChildItem2.NavigateUrl = "Products.aspx?MenuID=" + drchildDetails3["ID"].ToString();

ChildItem2.Selectable = true;

ChildItem1.ChildItems.Add(ChildItem2);

if (strSecChildItem1Product != strChildItem1Product)

{

strSecChildItem1Product = strChildItem1Product;

//SQLHelper ChildItem1ProductsDetails = new SQLHelper();

////SqlDataReader drChildItem1ProductsDetails = ChildItem1ProductsDetails.ExecuteReader("select ID,CategoryID,ProductName,Description from ProductsDetails where CategoryID='" + drchildDetails2["ID"].ToString() + "'");

//SqlDataReader drChildItem1ProductsDetails = ChildItem1ProductsDetails.ExecuteReader("select ID,CategoryID,ProductName,Description from ProductsDetails where CategoryID='" + strSecChildItem1Product + "'");

using (SqlConnection ConChildItem1ProductsDetails = new SqlConnection(strConnectionString))

{

SqlCommand CmdChildItem1ProductsDetails = new SqlCommand("select ID,CategoryID,ProductName,Description from ProductsDetails where CategoryID='" + strSecChildItem1Product + "'", ConChildItem1ProductsDetails);

ConChildItem1ProductsDetails.Open();

using(SqlDataReader drChildItem1ProductsDetails = CmdChildItem1ProductsDetails.ExecuteReader())

{

while (drChildItem1ProductsDetails.Read())

{

MenuItem ChildItem1Products = new MenuItem();

ChildItem1Products.Text = "" + drChildItem1ProductsDetails["ProductName"].ToString();

//ChildItem1Products.ToolTip = drChildItem1ProductsDetails["ProductName"].ToString();

ChildItem1Products.Value = drChildItem1ProductsDetails["ID"].ToString();

ChildItem1Products.NavigateUrl = "Products.aspx?MenuID=" + drChildItem1ProductsDetails["ID"].ToString();

ChildItem1.Selectable = true;

ChildItem2.ChildItems.Add(ChildItem1Products);

}

}

//OnceChildItem1Product = 1;

}

}

}

}

}

 

ChildItem.ChildItems.Add(ChildItem1);

if (strSecChildItemProduct != strChildItemProduct)

{

strSecChildItemProduct = strChildItemProduct;

//SQLHelper ChildItemProductsDetails = new SQLHelper();

////SqlDataReader drChildItemProductsDetails = ChildItemProductsDetails.ExecuteReader("select ID,CategoryID,ProductName,Description from ProductsDetails where CategoryID='" + drchildDetails1["ID"].ToString() + "'");

//SqlDataReader drChildItemProductsDetails = ChildItemProductsDetails.ExecuteReader("select ID,CategoryID,ProductName,Description from ProductsDetails where CategoryID='" + strSecChildItemProduct + "'");

using (SqlConnection ConChildItemProductsDetails = new SqlConnection(strConnectionString))

{

SqlCommand CmdChildItemProductsDetails = new SqlCommand("select ID,CategoryID,ProductName,Description from ProductsDetails where CategoryID='" + strSecChildItemProduct + "'", ConChildItemProductsDetails);

ConChildItemProductsDetails.Open();

using (SqlDataReader drChildItemProductsDetails = CmdChildItemProductsDetails.ExecuteReader())

{

while (drChildItemProductsDetails.Read())

{

MenuItem ChildItemProducts = new MenuItem();

ChildItemProducts.Text = "" + drChildItemProductsDetails["ProductName"].ToString();

//ChildItemProducts.ToolTip = drChildItemProductsDetails["ProductName"].ToString();

ChildItemProducts.Value = drChildItemProductsDetails["ID"].ToString();

ChildItemProducts.NavigateUrl = "Products.aspx?MenuID=" + drChildItemProductsDetails["ID"].ToString();

ChildItemProducts.Selectable = true;

ChildItem1.ChildItems.Add(ChildItemProducts);

}

}

//OnceChildItemProduct = 1;

}

}

#endregion

}

}

}

ParentItem.ChildItems.Add(ChildItem);

if (strSecParentItemProduct != strParentItemProduct)

{

strSecParentItemProduct = strParentItemProduct;

//SQLHelper ParentItemProductsDetails = new SQLHelper();

////SqlDataReader drParentItemProductsDetails = ParentItemProductsDetails.ExecuteReader("select ID,CategoryID,ProductName,Description from ProductsDetails where CategoryID='" + drparentDetails["ID"].ToString() + "'");

//SqlDataReader drParentItemProductsDetails = ParentItemProductsDetails.ExecuteReader("select ID,CategoryID,ProductName,Description from ProductsDetails where CategoryID='" + strSecParentItemProduct + "'");

using (SqlConnection ConParentItemProductsDetails = new SqlConnection(strConnectionString))

{

SqlCommand CmdParentItemProductsDetails = new SqlCommand("select ID,CategoryID,ProductName,Description from ProductsDetails where CategoryID='" + strSecParentItemProduct + "'", ConParentItemProductsDetails);

ConParentItemProductsDetails.Open();

using (SqlDataReader drParentItemProductsDetails = CmdParentItemProductsDetails.ExecuteReader())

{

while (drParentItemProductsDetails.Read())

{

MenuItem ParentItemProducts = new MenuItem();

ParentItemProducts.Text = drParentItemProductsDetails["ProductName"].ToString();

//ParentItemProducts.ToolTip = drParentItemProductsDetails["ProductName"].ToString();

ParentItemProducts.Value = drParentItemProductsDetails["ID"].ToString();

ParentItemProducts.NavigateUrl = "Products.aspx?MenuID=" + drParentItemProductsDetails["ID"].ToString();

ParentItemProducts.Selectable = true;

 

ChildItem.ChildItems.Add(ParentItemProducts);

}

}

//OnceParentItemProduct = 1;

}

}

#endregion

}

}

//MenuProducts.Items.Add(ParentItem);

}

mainMenuItem3.ChildItems.Add(ParentItem);

 

if (strSecMenuItemProductsID != strMenuItemProductsID)

{

strSecMenuItemProductsID = strMenuItemProductsID;

//SQLHelper MenuProductsDetails = new SQLHelper();

//SqlDataReader drMenuProductsDetails = MenuProductsDetails.ExecuteReader("select ID,CategoryID,ProductName,Description from ProductsDetails where CategoryID='" + strSecMenuItemProductsID + "'");

using (SqlConnection ConMenuProductsDetails = new SqlConnection(strConnectionString))

{

SqlCommand CmdMenuProductsDetails = new SqlCommand("select ID,CategoryID,ProductName,Description from ProductsDetails where CategoryID='" + strSecMenuItemProductsID + "'", ConMenuProductsDetails);

ConMenuProductsDetails.Open();

using (SqlDataReader drMenuProductsDetails = CmdMenuProductsDetails.ExecuteReader())

{

while (drMenuProductsDetails.Read())

{

MenuItem MenuItemProducts = new MenuItem();

MenuItemProducts.Text = drMenuProductsDetails["ProductName"].ToString();

//MenuItemProducts.ToolTip = drMenuProductsDetails["ProductName"].ToString();

MenuItemProducts.Value = drMenuProductsDetails["ID"].ToString();

MenuItemProducts.NavigateUrl = "Products.aspx?MenuID=" + drMenuProductsDetails["ID"].ToString();

MenuItemProducts.Selectable = true;

ParentItem.ChildItems.Add(MenuItemProducts);

}

}

//OnceMenuItemProduct = 1;

}

}

#endregion

}

}

 

MainMenu.Items.Add(mainMenuItem3);

MenuItem mainMenuItem4 = new MenuItem();

mainMenuItem4.Text = "Customers";

//mainMenuItem4.ToolTip = "Customers";

mainMenuItem4.NavigateUrl = "Customers.aspx";

mainMenuItem4.SeparatorImageUrl = "~/images/menu_line.gif";

MainMenu.Items.Add(mainMenuItem4);

MenuItem mainMenuItem5 = new MenuItem();

mainMenuItem5.Text = "News";

//mainMenuItem5.ToolTip = "News";

mainMenuItem5.NavigateUrl = "DisplayNews.aspx?NewsID=";

mainMenuItem5.SeparatorImageUrl = "~/images/menu_line.gif";

MainMenu.Items.Add(mainMenuItem5);

MenuItem mainMenuItem6 = new MenuItem();

mainMenuItem6.Text = "Enquiry";

//mainMenuItem6.ToolTip = "Enquiry";

mainMenuItem6.NavigateUrl = "Enquiry.aspx";

MainMenu.Items.Add(mainMenuItem6);

}

}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend