在.net的导航控件中,我用的最多的是TreeView控件,今天无意间用到了menu控件,感觉挺好的.写出来当作笔记吧!
前台代码:
<asp:Menu ID="Menu1" runat="server" BackColor="#FFFBD6" DynamicHorizontalOffset="2"
ForeColor="#990000" Height="187px" Orientation="Horizontal" StaticSubMenuIndent="10px"
Style="font-size: 8pt; font-family: Verdana; display: block; background-position-x: center; background-attachment: scroll; background-image: url(img/Alter.jpg); visibility: visible; color: #ff3399; background-repeat: repeat-x; position: static; background-color: #99ff00; text-align: center;" Font-Names="Verdana" Font-Size="0.8em" Width="594px" OnMenuItemClick="Menu1_MenuItemClick">
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicHoverStyle BackColor="#990000" ForeColor="White" />
<DynamicMenuStyle BackColor="#FFFBD6" BorderColor="White" BorderStyle="Solid" BorderWidth="1px" />
<StaticSelectedStyle BackColor="#FFCC66" />
<DynamicSelectedStyle BackColor="#FFCC66" />
<StaticHoverStyle BackColor="#990000" ForeColor="White" />
<DynamicMenuItemStyle BackColor="#B5C7DE" Font-Names="Verdana" Font-Size="8pt" BorderColor="White" BorderStyle="Solid" BorderWidth="1px" HorizontalPadding="5px" VerticalPadding="2px" />
</asp:Menu>
后台代码:
#region 页面初始化
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PR.BLL.menu.A_Menu bll = new PR.BLL.menu.A_Menu();
DataSet ds = bll.GetList();
CreateMenu(1, (MenuItem)null, ds);
}
}
#endregion
#region 递归生成横向菜单
public void CreateMenu(int ParentID, MenuItem tempItem, DataSet ds)
{
DataView dvMenu = new DataView(ds.Tables[0]);
//过滤ParentID,得到当前菜单的所有子节点
dvMenu.RowFilter = "[MenuParentId] = " + ParentID;
foreach (DataRowView Row in dvMenu)
{
if (tempItem == null)
{ //添加根节点
MenuItem Node = new MenuItem();
//节点的文本
Node.Text = Row["FMenuName"].ToString();
//节点旁边的图片
// Node.ImageUrl = "img/" + Row["MenuImage"].ToString();
//有子菜单时的图片
//Node.PopOutImageUrl = "img/02.gif";
this.Menu1.Items.Add(Node);
CreateMenu(Int32.Parse(Row["Fid"].ToString()), Node, ds);//再次递归
}
else
{ //添加当前节点的子节点
MenuItem Node = new MenuItem();
Node.Text = Row["FMenuName"].ToString();
//点击节点后的连接地址
Node.NavigateUrl = Row["Url"].ToString();
//Node.ImageUrl = "Image/" + Row["MenuImage"].ToString();
//Node.Target = Row["left"].ToString();
tempItem.ChildItems.Add(Node);
CreateMenu(Int32.Parse(Row["Fid"].ToString()), Node, ds); //再次递归
}
}
}
#endregion
前台代码:
<asp:Menu ID="Menu1" runat="server" BackColor="#FFFBD6" DynamicHorizontalOffset="2"
ForeColor="#990000" Height="187px" Orientation="Horizontal" StaticSubMenuIndent="10px"
Style="font-size: 8pt; font-family: Verdana; display: block; background-position-x: center; background-attachment: scroll; background-image: url(img/Alter.jpg); visibility: visible; color: #ff3399; background-repeat: repeat-x; position: static; background-color: #99ff00; text-align: center;" Font-Names="Verdana" Font-Size="0.8em" Width="594px" OnMenuItemClick="Menu1_MenuItemClick">
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicHoverStyle BackColor="#990000" ForeColor="White" />
<DynamicMenuStyle BackColor="#FFFBD6" BorderColor="White" BorderStyle="Solid" BorderWidth="1px" />
<StaticSelectedStyle BackColor="#FFCC66" />
<DynamicSelectedStyle BackColor="#FFCC66" />
<StaticHoverStyle BackColor="#990000" ForeColor="White" />
<DynamicMenuItemStyle BackColor="#B5C7DE" Font-Names="Verdana" Font-Size="8pt" BorderColor="White" BorderStyle="Solid" BorderWidth="1px" HorizontalPadding="5px" VerticalPadding="2px" />
</asp:Menu>
后台代码:
#region 页面初始化
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PR.BLL.menu.A_Menu bll = new PR.BLL.menu.A_Menu();
DataSet ds = bll.GetList();
CreateMenu(1, (MenuItem)null, ds);
}
}
#endregion
#region 递归生成横向菜单
public void CreateMenu(int ParentID, MenuItem tempItem, DataSet ds)
{
DataView dvMenu = new DataView(ds.Tables[0]);
//过滤ParentID,得到当前菜单的所有子节点
dvMenu.RowFilter = "[MenuParentId] = " + ParentID;
foreach (DataRowView Row in dvMenu)
{
if (tempItem == null)
{ //添加根节点
MenuItem Node = new MenuItem();
//节点的文本
Node.Text = Row["FMenuName"].ToString();
//节点旁边的图片
// Node.ImageUrl = "img/" + Row["MenuImage"].ToString();
//有子菜单时的图片
//Node.PopOutImageUrl = "img/02.gif";
this.Menu1.Items.Add(Node);
CreateMenu(Int32.Parse(Row["Fid"].ToString()), Node, ds);//再次递归
}
else
{ //添加当前节点的子节点
MenuItem Node = new MenuItem();
Node.Text = Row["FMenuName"].ToString();
//点击节点后的连接地址
Node.NavigateUrl = Row["Url"].ToString();
//Node.ImageUrl = "Image/" + Row["MenuImage"].ToString();
//Node.Target = Row["left"].ToString();
tempItem.ChildItems.Add(Node);
CreateMenu(Int32.Parse(Row["Fid"].ToString()), Node, ds); //再次递归
}
}
}
#endregion