首页 > 编程 > .NET > 正文

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

2024-07-10 12:57:36
字体:
来源:转载
供稿:网友


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



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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表