![]() |
Web Development »
ASP.NET Controls »
Menu Controls
Intermediate
License: The Code Project Open License (CPOL)
Binding Menu Control With DatabaseBy Ravi SelvarajBind the menu control with list of Category and Sub Categories |
Javascript, CSS, HTML, XHTML, .NET (.NET 2.0), ASP.NET, IIS (IIS 5.1), Visual Studio (VS2005), ADO.NET, WebForms, Dev
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
Download source - 4.83 KB
Figure 1

Figure 2
In this article I will show how to bind the menu control with the database. Suppose you have a situation to display the different Category and its Sub-Categories and when we click the Category/Sub Category (See Figure 1) to view the detail information about Category/Sub Category (See Figure 2), then this bit of code will be useful. This code prevents a post back to the server while selecting Sub Category.
Database Detail
Table Name - Category

Table Name - SubCategory
There are two tables(Category,SubCategory) which are related to each other.
Here’s the method called BindMenu, which binds the menu control during the page load.
1) Create the connection string to establish connection with the database
Dim connectionString As String =
WebConfigurationManager.ConnectionStrings("DatabaseConnectionString1").ConnectionString
Dim con As New SqlConnection(connectionString)
Dim dadCategories As New SqlDataAdapter("SELECT CatId,CatName FROM Category
order by CatName", con)
Dim dadSubCat As New SqlDataAdapter("SELECT SubCatId,CatId,SubCatName FROM
SubCategory order by SubCatName", con)
Dim dsCat As New DataSet()
Using con
con.Open()
dadCategories.Fill(dsCat, "Category")
dadSubCat.Fill(dsCat, "SubCategory")
End Using
dsCat.Relations.Add("Children", dsCat.Tables("Category").Columns("CatId"),
dsCat.Tables("SubCategory").Columns("CatId"))
For Each categoryRow As DataRow In dsCat.Tables("Category").Rows
Dim mNode As New MenuItem(CType(categoryRow("CatName"), String), "", "",
"~/DetailView.aspx?CatID=" + CType(categoryRow("CatId"), String), "_parent")
Menu1.Items.Add(mNode)
Dim subCatRows() As DataRow = categoryRow.GetChildRows("Children")
For Each row As DataRow In subCatRows
Dim subCatName As String = CType(row("SubCatName"), String)
Dim subCatItems As New MenuItem(subCatName, "", "",
"~/DetailView.aspx?CatID=" + CType(row("CatId"), String) + "&SubCatID=" +
CType(row("SubCatId"), String), "_parent")
Menu1.Items(count).ChildItems.Add(subCatItems)
Next
count = count + 1
Next
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 28 Jun 2008 Editor: |
Copyright 2008 by Ravi Selvaraj Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |