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不定级动态菜单的实现--------------------------------------------------------------------------------htt...
  • hymxtang
  • hymxtang
  • 2006-09-23 13:04:00
  • 630

ASP.NET MVC4 +EF+ EasyUI实现 动态菜单

这段时间没事,做了一个动态菜单的实现。有很多的地方想的不是很全,做的不够完善。欢迎大神们拍砖和指点! 话不多说,进入正题: 设计了一个数据库表: 在设计的时候每一个菜单的子菜单最多能添加 99 个,子...
  • shan1774965666
  • shan1774965666
  • 2015-02-27 14:55:40
  • 2674

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

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

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

  前日,根据客户需要,要根据如下的表动态生成如下的菜单:表中的数据是动态更新的。    我们平时IEWebContr来做菜单时都是在html中直接输入,也就是说是静态的,现在要做成动态的,而且菜单级...
  • zjjgs
  • zjjgs
  • 2006-09-21 10:54:00
  • 656

ASP.net中动态导航菜单的实现

使用ASP.net开发网站时,有时需要根据不同的用户权限显示不同的导航菜单(Navigation Menu)。 在VS2008的【工具箱】->【导航】拖动一个Menu控件到test.aspx页面,将I...
  • donhao
  • donhao
  • 2011-07-15 09:27:34
  • 2733

让菜单充满活力:ASP.NET根据角色动态分配菜单+权限

这次做图书馆维护系统,首先要解决的问题就是角色权限动态分配,权限分配直接体现就是菜单的动态分配。在此和大家分享一下心得。    大多数系统,都有多种类型的用户,不同的用户权限不同,某一个功能,A类用...
  • yangyuankp
  • yangyuankp
  • 2012-05-20 19:06:54
  • 8142

asp.net 连数据库动态生成menu中的菜单项

第一步 建表create table TreeTest( id int identity(1,1) primary key , treeId int,  --数形结构 1,代表根目录 2,代表根的下一...
  • liusen5555
  • liusen5555
  • 2007-09-10 16:02:00
  • 5398

asp.net 动态菜单

  • 2011年05月06日 15:36
  • 133KB
  • 下载

asp.net动态js菜单(动态菜单+静态菜单+数据库)

  • 2012年12月03日 15:59
  • 13.13MB
  • 下载

MVC动态获取树形菜单信息

菜单效果如图: 后台递归得到菜单树代码: /// /// 获取菜单树 /// /// 菜单父节点ID /// ...
  • q646926099
  • q646926099
  • 2016-08-10 14:07:30
  • 4632
收藏助手
不良信息举报
您举报文章:ASP.NET中不定级动态菜单的实现(附图)
举报原因:
原因补充:

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