ASP.NET中不定级动态菜单的实现(附图)

原创 2004年09月02日 11:44:00

        前日,根据客户需要,要根据如下的表动态生成如下的菜单:表中的数据是动态更新的。

     

        我们平时IEWebContr来做菜单时都是在html中直接输入,也就是说是静态的,现在要做成动态的,而且菜单级数不定,怎么来实现呢?笔者通过研究发现,用 IEWebContr来做菜单时,菜单项可以直接输入,也可以用如下的方法指定从某一个文件中读取菜单项: P>

〈iewc:TreeView runat="server" ID="Treeview1" NAME="Treeview1"〉
〈iewc:TreeNode id="Treenode1" runat="server" TreeNodeSrc="menuone.txt" Expanded="True" Text="信息类别列表" 〉〈/iewc:TreeNode〉
〈/iewc:TreeView〉

         其中menuone.txt的内容如下:

〈TREENODES〉
〈treenode text='蔬菜' NavigateUrl='xxcb_add.aspx?classname=蔬菜&classid=01'? Target='main1'〉
〈treenode text='白菜' NavigateUrl='xxcb_add.aspx?classname=白菜&classid=0101'?? Target='main1'/〉
〈treenode text='萝卜' NavigateUrl='xxcb_add.aspx?classname=萝卜&classid=0102'?? Target='main1'/〉
〈treenode text='黄花菜' NavigateUrl='xxcb_add.aspx?classname=黄花菜&classid=0103'?? Target='main1'/〉
〈/treenode〉
〈treenode text='水果' NavigateUrl='xxcb_add.aspx?classname=水果&classid=02'? Target='main1'〉
〈treenode text='苹果' NavigateUrl='xxcb_add.aspx?classname=苹果&classid=0201'? Target='main1'〉
〈treenode text='红富士' NavigateUrl='xxcb_add.aspx?classname=红富士&classid=020101'?? Target='main1'/〉
〈treenode text='其它苹果' NavigateUrl='xxcb_add.aspx?classname=其它苹果&classid=020102'?? Target='main1'/〉
〈/treenode〉
〈treenode text='桃子' NavigateUrl='xxcb_add.aspx?classname=桃子&classid=0202'?? Target='main1'/〉
〈treenode text='荔枝' NavigateUrl='xxcb_add.aspx?classname=荔枝&classid=0203'?? Target='main1'/〉
〈/treenode〉
〈treenode text='粮食' NavigateUrl='xxcb_add.aspx?classname=粮食&classid=03'? Target='main1'〉
〈treenode text='大米' NavigateUrl='xxcb_add.aspx?classname=大米&classid=0301'?? Target='main1'/〉
〈treenode text='玉米' NavigateUrl='xxcb_add.aspx?classname=玉米&classid=0302'?? Target='main1'/〉
〈/treenode〉
〈/TREENODES〉

     &n bsp;现在的问题变成了如何将数据表根据“类别号”修改menuone.txt文件。将该过程放在对数据表修改后进行。下面是根据表的内容生成menuone.txt的源代码:
Imports System.IO
       Dim txtwriter As StreamWriter
       txtwriter = File.CreateText(Server.MapPath("menuone.txt"))
      txtwriter.WriteLine("<TREENODES>")


        Dim objConn As OleDbConnection
        Dim objCmd As OleDbCommand
        Dim objRead As OleDbDataReader
        Dim strSQL, strDSN As String
        Dim strid, strname, strid1, strname1 As String
        Dim strand As String
        strand = "&amp;"

        Dim i, j, k, m As Integer
        i = 0
        j = 0
        k = 0
        m = 0

        strSQL = "select * from class order by classid "

        objConn = New OleDbConnection(ConfigurationSettings.AppSettings("connString"))
        objCmd = New OleDbCommand(strSQL, objConn)
        objCmd.Connection.Open()
        objRead = objCmd.ExecuteReader()

        While objRead.Read()

            If j = 0 Then
                j = Len(Trim(objRead.Item("classid")))
            Else
                strid = strid1
                strname = strname1
                i = Len(Trim(objRead.Item("classid")))

                k = i - j

                Select Case k
                    Case Is > 0
                        txtwriter.WriteLine("<treenode text='" & strname & "' NavigateUrl='adminclass_add.aspx?classname=" & strname & "" & strand & "classid=" & strid & "'  Target='main2'>")
                    Case Is = 0
                        txtwriter.WriteLine("<treenode text='" & strname & "' NavigateUrl='adminclass_add.aspx?classname=" & strname & "" & strand & "classid=" & strid & "'   Target='main2'/>")
                    Case Is < 0
                        txtwriter.WriteLine("<treenode text='" & strname & "' NavigateUrl='adminclass_add.aspx?classname=" & strname & "" & strand & "classid=" & strid & "'   Target='main2'/>")
                        'txtwriter.WriteLine("</treenode>")

                        m = k
                        While m < 0
                            txtwriter.WriteLine("</treenode>")
                            m = m + 2
                        End While

                End Select

                j = Len(Trim(objRead.Item("classid")))

            End If

            strid1 = Trim(objRead.Item("classid"))
            strname1 = Trim(objRead.Item("classname"))

        End While

        'strid = strid1
        'strname = strname1
        i = Len(strid)
        k = j - i
        strid = strid1
        strname = strname1
        Select Case k
            Case Is > 0
                txtwriter.WriteLine("<treenode text='" & strname & "' NavigateUrl='adminclass_add.aspx?classname=" & strname & "" & strand & "classid=" & strid & "'   Target='main2'/>")
                'txtwriter.WriteLine("</treenode>")

                m = Len(strid) / 2

                While m > 1
                    txtwriter.WriteLine("</treenode>")
                    m = m - 1
                End While
            Case Is = 0
                txtwriter.WriteLine("<treenode text='" & strname & "' NavigateUrl='adminclass_add.aspx?classname=" & strname & "" & strand & "classid=" & strid & "'   Target='main2'/>")
                'txtwriter.WriteLine("</treenode>")
                m = Len(strid) / 2

                While m > 1
                    txtwriter.WriteLine("</treenode>")
                    m = m - 1
                End While
            Case Is <= 0
                txtwriter.WriteLine("<treenode text='" & strname & "' NavigateUrl='adminclass_add.aspx?classname=" & strname & "" & strand & "classid=" & strid & "'   Target='main2'/>")
                'txtwriter.WriteLine("</treenode>")

                m = Len(strid) / 2

                While m > 1
                    txtwriter.WriteLine("</treenode>")
                    m = m - 1
                End While

        End Select
        txtwriter.WriteLine("</TREENODES>")
        txtwriter.Close()
    End Sub
    Sub creatmenuone(ByVal strpath As String)
        Dim txtwriter As StreamWriter
        txtwriter = File.CreateText(strpath + ("/../menuone.txt"))
        txtwriter.WriteLine("<TREENODES>")


        Dim objConn As OleDbConnection
        Dim objCmd As OleDbCommand
        Dim objRead As OleDbDataReader
        Dim strSQL, strDSN As String
        Dim strid, strname, strid1, strname1 As String
        Dim strand As String
        strand = "&amp;"

        Dim i, j, k, m As Integer
        i = 0
        j = 0
        k = 0
        m = 0

        strSQL = "select * from class order by classid "

        objConn = New OleDbConnection(ConfigurationSettings.AppSettings("connString"))
        objCmd = New OleDbCommand(strSQL, objConn)
        objCmd.Connection.Open()
        objRead = objCmd.ExecuteReader()

        While objRead.Read()

            If j = 0 Then
                j = Len(Trim(objRead.Item("classid")))
            Else
                strid = strid1
                strname = strname1
                i = Len(Trim(objRead.Item("classid")))

                k = i - j

                Select Case k
                    Case Is > 0
                        txtwriter.WriteLine("<treenode text='" & strname & "' NavigateUrl='xxcb_add.aspx?classname=" & strname & "" & strand & "classid=" & strid & "'  Target='main1'>")
                    Case Is = 0
                        txtwriter.WriteLine("<treenode text='" & strname & "' NavigateUrl='xxcb_add.aspx?classname=" & strname & "" & strand & "classid=" & strid & "'   Target='main1'/>")
                    Case Is < 0
                        txtwriter.WriteLine("<treenode text='" & strname & "' NavigateUrl='xxcb_add.aspx?classname=" & strname & "" & strand & "classid=" & strid & "'   Target='main1'/>")
                        'txtwriter.WriteLine("</treenode>")

                        m = k
                        While m < 0
                            txtwriter.WriteLine("</treenode>")
                            m = m + 2
                        End While

                End Select

                j = Len(Trim(objRead.Item("classid")))

            End If

            strid1 = Trim(objRead.Item("classid"))
            strname1 = Trim(objRead.Item("classname"))

        End While

        'strid = strid1
        'strname = strname1
        i = Len(strid)
        k = j - i
        strid = strid1
        strname = strname1
        Select Case k
            Case Is > 0
                txtwriter.WriteLine("<treenode text='" & strname & "' NavigateUrl='xxcb_add.aspx?classname=" & strname & "" & strand & "classid=" & strid & "'   Target='main1'/>")
                'txtwriter.WriteLine("</treenode>")

                m = Len(strid) / 2

                While m > 1
                    txtwriter.WriteLine("</treenode>")
                    m = m - 1
                End While
            Case Is = 0
                txtwriter.WriteLine("<treenode text='" & strname & "' NavigateUrl='xxcb_add.aspx?classname=" & strname & "" & strand & "classid=" & strid & "'   Target='main1'/>")
                'txtwriter.WriteLine("</treenode>")
                m = Len(strid) / 2

                While m > 1
                    txtwriter.WriteLine("</treenode>")
                    m = m - 1
                End While
            Case Is <= 0
                txtwriter.WriteLine("<treenode text='" & strname & "' NavigateUrl='xxcb_add.aspx?classname=" & strname & "" & strand & "classid=" & strid & "'   Target='main1'/>")
                'txtwriter.WriteLine("</treenode>")

                m = Len(strid) / 2

                While m > 1
                    txtwriter.WriteLine("</treenode>")
                    m = m - 1
                End While

        End Select
        txtwriter.WriteLine("</TREENODES>")
        txtwriter.Close()

     这样menuone.txt就按要求生成了菜单源文件。
    如有更好的办法,请与我联系谢谢。QQ:149592886

ASP.NET中不定级动态菜单的实现

ASP.NET中不定级动态菜单的实现 我们平时IEWebContr来做菜单时都是在html中直接输入,也就是说是静态的,现在要做成动态的,而且菜单级数不定,怎么来实现呢?笔者通过研究发现,用...
  • u014739765
  • u014739765
  • 2014年04月23日 16:55
  • 506

ASP.NET+SQL SERVER 动态加载菜单详解

ASP.NET + 三层架构 + sql server ,从数据库遍历树形表,实现无限级生成动态菜单。...
  • Amazing_Pei
  • Amazing_Pei
  • 2016年08月18日 15:25
  • 1365

216-----xml读取,树形菜单展开,收缩

树状导航菜单显示人事组织架构 a:hover { font-size: 10pt; color: #FF6600; vertical-align:middle; } a { font-siz...
  • qq_34242581
  • qq_34242581
  • 2017年01月17日 22:44
  • 324

ASP.net Menu控件动态添加菜单项实现权限控制

对于动态菜单生成,在很多具有权限控制的项目中非常常见。最近遇到一个需求就是不同用户具有不同权限,该权限是页面级的,所以需要根据不同用户的权限情况动态生成菜单项。        首先,我把菜单项中的内...
  • dota_371581
  • dota_371581
  • 2014年03月20日 14:44
  • 1709

MVC5+EF6 入门完整教程13 -- 动态生成多级菜单

稍微有一定复杂性的系统,多级菜单都是一个必备组件。 本篇专题讲述如何生成动态多级菜单的通用做法。 我们不用任何第三方的组件,完全自己构建灵活通用的多级菜单。 需要达成的效果:容易复...
  • mss359681091
  • mss359681091
  • 2016年08月06日 13:44
  • 3018

easyUI动态生成多级菜单树

  • Seri_com
  • Seri_com
  • 2017年03月14日 22:09
  • 419

Asp.net 用datalist嵌套的方法实现二级菜单的分类导航

刚开始学习Asp.net做网站的时候, 做的是一个电子图书购买网站,发现图书有多级类目。   例如:小说分类下面世界名著,中国古典小说.......  文学类目下有 文学理论 中国古典诗歌。 这些要是...
  • u012260740
  • u012260740
  • 2014年03月13日 21:20
  • 1131

动态菜单----动态生成菜单和java反射机制的使用

上一篇博文中介绍了java的反射机制的原理和基本功能,读后肯定对反射机制有一定的了解,本篇博文将以我的项目要求为例子,讲述动态菜单的生成和java反射机制的使用。 任务要求:所有的菜单项都从数据库中取...
  • WCCchangchun
  • WCCchangchun
  • 2015年08月21日 16:45
  • 2191

研发人员技术定级的一些思考

所谓对研发人员技术定级,就是指给研发人员定义哪些级别。为什么要给研发人员定级?这里面有多方面的作用: 一是定级是薪资管理的一部分,不同级别的开发人员对应不同的薪资水平。 二是定级是从技术管理的方便考虑...
  • clever101
  • clever101
  • 2014年05月17日 22:06
  • 3514

asp.net二级菜单导航实例1

  • yayun0516
  • yayun0516
  • 2014年12月08日 20:18
  • 1200
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ASP.NET中不定级动态菜单的实现(附图)
举报原因:
原因补充:

(最多只允许输入30个字)