我已经用递归从数据库中读取了分层数据,我想问:怎么将数据绑定到控件以导航形式显示出来?
有人能给我一点建议或告诉我怎么做么?
数据库: menu.mdf
表 : menu_table
表结构:
id menu_name parent_id
1 home 0
2 news 0
3 about 0
4 introduciton 1
5 localnews 2
6 globalnews 2
7 contact 3
这是后台代码:
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
/*
* Recusive the menu from database
*/
public partial class About : System.Web.UI.Page
{
public static string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath("~/App_Data/menu.mdb");
OleDbConnection conn = new OleDbConnection(connstring);
protected void Page_Load(object sender, EventArgs e)
{
this.menu.Text = queryAllMenu(0, conn);
}
public string queryAllMenu(int id, OleDbConnection conn)
{
string Result = id.ToString();
string cmd_Sql = "SELECT * FROM menu_table WHERE parent_id =" + id;
try
{
DataSet ds = new DataSet();
OleDbDataAdapter cmd = new OleDbDataAdapter(cmd_Sql, conn);
cmd.Fill(ds, "menu");
DataTable dt = ds.Tables["menu"];
for (int i = 0; i < dt.Rows.Count; i++)
{
int menu_num = Convert.ToInt32(dt.Rows[i][0].ToString());
if ("".Equals(Result))
Result += menu_num;
else
Result += ", " +dt.Rows[i]["menu_name"] + "<br />";
string sub_menu = menuChild(menu_num, conn);
if (!"".Equals(sub_menu))
Result += sub_menu+ "<br />";
}
if (conn.State != ConnectionState.Closed)
conn.Close();
}
catch
{
return Result;
}
return Result;
}
private string menuChild(int Parent_Id, OleDbConnection conn)
{
string Result = string.Empty;
string cmdSql = "SELECT * FROM menu_table WHERE parent_id =" + Parent_Id;
try
{
DataSet ds = new DataSet();
OleDbDataAdapter cmd = new OleDbDataAdapter(cmdSql, conn);
cmd.Fill(ds, "dropdown_menu");
DataTable dt = ds.Tables["dropdown_menu"];
for (int i = 0; i < dt.Rows.Count; i++)
{
int id = Convert.ToInt32(dt.Rows[i][0].ToString());
if ("".Equals(Result))
Result += dt.Rows[i]["menu_name"];
else
Result += "," + dt.Rows[i]["menu_name"];
Result += menuChild(id, conn);
}
if (conn.State != ConnectionState.Closed)
conn.Close();
}
catch
{
return Result;
}
return Result;
}
}