如何将数据绑定到菜单或树形视图? [英] How do i bind the data to menu or treeview?
问题描述
大家好,
我是新来的人.
这是我第一次发布问题.
希望得到一些答案和建议.
我想制作一个菜单用户控件,该控件可以显示数据库中的n级子类别.
首先,我已经使用递归方法检索了数据.
接下来,我想像Codeproject一样使用菜单显示数据,我对此有疑问.
如果有人可以帮助我解决困扰我的问题,我将不胜感激.
这是我的数据表:
Hi everybody,
I am a new guy.
This is the first time I have posted a question.
I hope get some answers and advices.
I want to make a menu user control which can display n-level sub-categories from database.
Firstly, I have already retrieved the data with recursion method.
Next, I want to display the data with menu just like the Codeproject''s, and I have a problem with that.
If anybody can help me to solve this problem which bothers me some times, I''ll appreciate.
This is my data table:
id menu_name parent_id
1 home 0
2 news 0
3 about 0
4 introduction 1
5 localnews 2
6 globalnews 2
7 contact 3
这是我的代码:
This is my code:
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)
{
//bind the data with label
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;
}
}
[edit]将内联代码转换为代码块-OriginalGriff [/edit]
[edit]Inline codes converted to Code blocks - OriginalGriff[/edit]
推荐答案
新人,
请查看以下帖子:使用ASP.NET 2.0构建数据库驱动的分层菜单 [
Hi new guy,
Please have a look at the following post: Building a Database Driven Hierarchical Menu using ASP.NET 2.0[^]
Kind regards,
这篇关于如何将数据绑定到菜单或树形视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!