明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

ASP.NET中不定級(jí)動(dòng)態(tài)菜單的完成

[摘要]作者:xiangwa 出處:CSDN 前日,根據(jù)客戶需要,要根據(jù)如下的表動(dòng)態(tài)生成如下的菜單:表中的數(shù)據(jù)是動(dòng)態(tài)更新的。   我們平時(shí)IEWebContr來做菜單時(shí)都是在html中直接輸入,也就是說是靜態(tài)的,現(xiàn)在要做成動(dòng)態(tài)的,而且菜單級(jí)數(shù)不定,怎么來實(shí)現(xiàn)呢?筆者通過研究發(fā)現(xiàn),用IEWebContr...

作者:xiangwa  出處:CSDN

 前日,根據(jù)客戶需要,要根據(jù)如下的表動(dòng)態(tài)生成如下的菜單:表中的數(shù)據(jù)是動(dòng)態(tài)更新的。 

  我們平時(shí)IEWebContr來做菜單時(shí)都是在html中直接輸入,也就是說是靜態(tài)的,現(xiàn)在要做成動(dòng)態(tài)的,而且菜單級(jí)數(shù)不定,怎么來實(shí)現(xiàn)呢?筆者通過研究發(fā)現(xiàn),用IEWebContr來做菜單時(shí),菜單項(xiàng)可以直接輸入,也可以用如下的方法指定從某一個(gè)文件中讀取菜單項(xiàng):

   <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的內(nèi)容如下:

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

  現(xiàn)在的問題變成了如何將數(shù)據(jù)表根據(jù)“類別號(hào)”修改menuone.txt文件。將該過程放在對(duì)數(shù)據(jù)表修改后進(jìn)行。下面是根據(jù)表的內(nèi)容生成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 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就按要求生成了菜單源文件。