分享
 
 
 

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

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

我们平时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〉

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有