前年写了一遍 Vs 2005 treeview无限循环的文章http://blog.csdn.net/SpriteNet/archive/2008/04/02/2245363.aspx,最后提到说稍微修改可以用到 menu 控件上,最近项目用到了menu的绑定数据库,思路和treeview一样,也是递归的办法,具体如何实现,咱们下次再说,先吃饭,占个地方。。。。。。。(*^__^*) 嘻嘻……
///
回来了,咱们继续
先说表结构吧,表结构其实和《Vs 2005 treeview无限循环》的结构是一样的,不过就是多了一个记录url的字段,大家可以看看《Vs 2005 treeview无限循环》的内容。
以下是相关代码:
1.皮肤样式
<%--Menu--%>
<asp:Menu runat="server" SkinID="MgeShow"
MaximumDynamicDisplayLevels="3" Orientation="Horizontal" StaticDisplayLevels="2"
BackColor="#396395" Font-Overline="False" ForeColor="#E0E0E0" Font-Size="30pt" StaticSubMenuIndent="" StaticEnableDefaultPopOutImage="False" Font-Bold="True">
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="4px"/>
<DynamicHoverStyle BackColor="#7AB0EF" ForeColor="#ffffff" />
<DynamicMenuStyle BackColor="#4982C5" BorderColor="#396395" BorderStyle="Outset" BorderWidth="1px" ForeColor="#FF7E00"/><%--Height="30px" HorizontalPadding="10px"--%>
<StaticSelectedStyle BackColor="#396395" ForeColor="#E0E0E0" /> <%--无二级菜单时的一级样式--%>
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="4px" Font-Strikeout="False" Height="24px" />
<StaticHoverStyle BackColor="#4982C5" ForeColor="#ffffff" /> <%--导航第一级鼠标放上背景颜色和字体颜色--%>
</asp:Menu>
2.cs
protected void initMenu()
{
DataTable dt = GetViewTable();
DataView dv = new DataView(dt);
dv.RowFilter = "QB_Fid='0'";
foreach (DataRowView drv in dv)
{
MenuItem node = new MenuItem();
node.Text = drv["QB_Name"].ToString();
node.Value = drv["QB_Id"].ToString();
node.NavigateUrl = drv["QB_Url"].ToString().Trim();
Menu1.Items.Add(node);
AddNodes(dt, node);
}
}
protected DataTable GetViewTable()
{
string strConn = System.Configuration.ConfigurationManager.AppSettings["strconnC"];
SqlConnection myConn = new SqlConnection(strConn);
string strSql = "select * from QX_Base where QB_JY=0 order by QB_Fid,QB_Sort";
SqlDataAdapter myDa = new SqlDataAdapter(strSql, myConn);
DataTable dt = new DataTable();
myDa.Fill(dt);
return dt;
}
/// <summary>
/// 子节点添加
/// </summary>
/// <param name="dt">数据表</param>
/// <param name="pNode"></param>
protected void AddNodes(DataTable dt, MenuItem pNode)
{
DataView dv = new DataView(dt);
dv.RowFilter = "QB_Fid =" + pNode.Value ;
foreach (DataRowView drv in dv)
{
MenuItem menuNode = new MenuItem();
menuNode.Text = drv["QB_Name"].ToString();
menuNode.NavigateUrl = drv["QB_Url"].ToString().Trim();
menuNode.Value = drv["QB_Id"].ToString();
pNode.ChildItems.Add(menuNode);
//loop循环
AddNodes(dt, menuNode);
}
}