如何从数据库添加菜单项. [英] How to add menu items from Database.

查看:97
本文介绍了如何从数据库添加菜单项.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我想创建一个菜单,并且菜单项应该来自数据库.

Hello,
I want to create a menu and the menu items should come from the database.

推荐答案

创建GetMenuData的方法,以从数据库到数据集读取菜单数据. />
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();
}



在此处获取带有源代码的替代方法: http://www.codeguru.com/csharp/csharp/cs_misc/designtechniques/article.php/c15661 [ ^ ]



Get an alternative here with source code:http://www.codeguru.com/csharp/csharp/cs_misc/designtechniques/article.php/c15661[^]


检查这些博客
http://www.silverstripe.org/data-model-questions/show/17949 [ ^ ]
http://aspalliance.com/822 [ ^ ]
--NDK
check these blogs
http://www.silverstripe.org/data-model-questions/show/17949[^]
http://aspalliance.com/822[^]
--NDK


这篇关于如何从数据库添加菜单项.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆