程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> ASP.NET中不定級動態菜單的實現

ASP.NET中不定級動態菜單的實現

編輯:.NET實例教程

前日,根據客戶需要,要根據如下的表動態生成如下的菜單:表中的數據是動態更新的。 

  我們平時IEWebContr來做菜單時都是在Html中直接輸入,也就是說是靜態的,現在要做成動態的,而且菜單級數不定,怎麼來實現呢?筆者通過研究發現,用IEWebContr來做菜單時,菜單項可以直接輸入,也可以用如下的方法指定從某一個文件中讀取菜單項:

   <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>assid "

  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

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

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

  這樣menuone.txt就按要求生成了菜單源文件。


  </TREENODES>
 

  現在的問題變成了如何將數據表根據“類別號”修改menuone.txt文件。將該過程放在對數據表修改後進行。下面是根據表的內容生成menuone.txt的源代碼:


   Imports System.IO
  Dim txtwriter As StreamWriter
  txtwriter = File.CreateText(Server.MapPathstrpath ("\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 cl

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved