本文介绍了问题与菜单plz整个阅读有关.....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,即与菜单有关,实际上我希望在运行时生成菜单和子菜单.为此我应该怎么做.我使用了一个代码,因为我在表中有2个菜单,并且有关于一个但未显示的4个菜单.它一次仅显示一个菜单..而另一个菜单在另一个菜单中显示.im使用以下代码:>
i have a problem i.e. related to menus actually i want that at run time menu and submenu should be generated.what shoul i do for that.i have used one code in that i have 2 menus taht are in table and there are 4 menus regarding to one but its not showing.its showing only one at a time.. and another one is showing for another menu.i m using this code:>
void menu_MenuItemClick(object sender, MenuEventArgs e)
{
string selected = e.Item.Text;
//SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter adCat = new SqlDataAdapter("SELECT menu_id FROM menu where menu_name='" + selected + "'", con);
DataTable dt = new DataTable();
adCat.Fill(dt);
int id = Convert.ToInt32(dt.Rows[0]["menu_id"].ToString());
//Response.Redirect("Default2.aspx?id=" + id);
}
private void PopulateMenu()
{
DataSet ds = GetDataSetForMenu();
Menu menu = new Menu();
menu.MenuItemClick += new MenuEventHandler(menu_MenuItemClick);
foreach (DataRow parentItem in ds.Tables["menu"].Rows)
{
MenuItem categoryItem = new MenuItem((string)parentItem["menu_name"]);
menu.Items.Add(categoryItem);
foreach (DataRow childItem in parentItem.GetChildRows("Children"))
{
MenuItem childrenItem = new MenuItem((string)childItem["submenu_name"]);
categoryItem.ChildItems.Add(childrenItem);
}
}
Panel1.Controls.Add(menu);
Panel1.DataBind();
}
private DataSet GetDataSetForMenu()
{
SqlDataAdapter adCat = new SqlDataAdapter("SELECT * FROM menu", con);
SqlDataAdapter adProd = new SqlDataAdapter("SELECT * FROM submenu", con);
DataSet ds = new DataSet();
adCat.Fill(ds,"menu");
adProd.Fill(ds,"submenu");
ds.Relations.Add("Children", ds.Tables["menu"].Columns["menu_id"], ds.Tables["submenu"].Columns["submenu_id"],false);
return ds;
}
并在页面加载此方法,即PopulateMenu();
and at page load this method i.e. PopulateMenu(); is called.
推荐答案
foreach (DataRow parentItem in ds.Tables["menu"].Rows)
{
MenuItem categoryItem = new MenuItem((string)parentItem["menu_name"]);
foreach (DataRow childItem in parentItem.GetChildRows("Children"))
{
MenuItem childrenItem = new MenuItem((string)childItem["submenu_name"]);
categoryItem.ChildItems.Add(childrenItem);
}
menu.Items.Add(categoryItem);
//you've to add categoryitem after adding childitems to it
}
一次检查
最好的
check it once
All the Best
这篇关于问题与菜单plz整个阅读有关.....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!