[问题] 从SQL字段判来产生Menu

楼主: qwerty147852 (小城主)   2018-05-28 19:34:26
请教各位大大
目前在画面上做了写死的动态menu
web:https://imgur.com/D2ceHQW
code: https://imgur.com/mikTb4r
SQL table:https://imgur.com/lX9J3W1
希望能改用从SQL来的资料做menu能依照table里字段(单位编号=001 & 执行状态=1 显示)
目前想到作法是这样,没写过VB不知道可不可行,又或者有其他建议?
1.对SQL做逐步select 有效状态的table(单位编号=001 & 执行状态=1 显示)
2.取出有效资料代入子表中
3.子表跟母表进行合并
4.返回GetDate()
请问该如何改asp.net下面的code?
Imports System.Data.SqlClient
Imports System.Data
Partial Class VBASPNETMenu
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not IsPostBack Then
GenerateMenuItem()
End If
End Sub
Public Sub GenerateMenuItem()
' 从数据库中获取数据。
Dim ds As DataSet = GetData()
For Each mainRow As DataRow In ds.Tables(0).Rows
' 将主表中的记录加载到菜单控件。
Dim masterItem As New MenuItem(mainRow("mainName").ToString())
masterItem.NavigateUrl = mainRow("mainUrl").ToString()
Menu1.Items.Add(masterItem)
Menu1.Visible = True
For Each childRow As DataRow In mainRow.GetChildRows("Child")
' 根据主表和子表的关系,从子表中加载数据。
Dim childItem As New
MenuItem(DirectCast(childRow("childName"), String))
childItem.NavigateUrl = childRow("childUrl").ToString()
masterItem.ChildItems.Add(childItem)
Next
Next
End Sub
Public Function GetData() As DataSet
'SQL???循环跑子表???
' 为了测试,使用内存表作为数据源。
Dim mainTB As New DataTable()
Dim mainIdCol As New DataColumn("mainId")
Dim mainNameCol As New DataColumn("mainName")
Dim mainUrlCol As New DataColumn("mainUrl")
mainTB.Columns.Add(mainIdCol)
mainTB.Columns.Add(mainNameCol)
mainTB.Columns.Add(mainUrlCol)
Dim childTB As New DataTable()
Dim childIdCol As New DataColumn("childId")
Dim childNameCol As New DataColumn("childName")
' 子表的MainId列是主表的外键。
Dim childMainIdCol As New DataColumn("MainId")
Dim childUrlCol As New DataColumn("childUrl")
childTB.Columns.Add(childIdCol)
childTB.Columns.Add(childNameCol)
childTB.Columns.Add(childMainIdCol)
childTB.Columns.Add(childUrlCol)
' 将一些测试记录插入到主表中。
Dim dr As DataRow = mainTB.NewRow()
dr(0) = "1"
dr(1) = "菜单管理"
dr(2) = "Test.aspx"
mainTB.Rows.Add(dr)
Dim dr1 As DataRow = mainTB.NewRow()
dr1(0) = "2"
dr1(1) = "新人训练"
dr1(2) = "Test.aspx"
mainTB.Rows.Add(dr1)
Dim dr2 As DataRow = mainTB.NewRow()
dr2(0) = "3"
dr2(1) = "薪俸发放"
dr2(2) = "Test.aspx"
mainTB.Rows.Add(dr2)
Dim dr3 As DataRow = mainTB.NewRow()
dr3(0) = "4"
dr3(1) = "食材管理"
dr3(2) = "Test.aspx"
mainTB.Rows.Add(dr3)
Dim dr4 As DataRow = mainTB.NewRow()
dr4(0) = "5"
dr4(1) = "人事管理"
dr4(2) = "Test.aspx"
mainTB.Rows.Add(dr4)
'将一些测试记录插入到子表中
Dim dr_child1 As DataRow = childTB.NewRow()
dr_child1(0) = "0"
dr_child1(1) = "1.T0101"
dr_child1(2) = "1"
dr_child1(3) = "Test.aspx"
childTB.Rows.Add(dr_child1)
Dim dr_child2 As DataRow = childTB.NewRow()
dr_child2(0) = "1"
dr_child2(1) = "2.T0103"
dr_child2(2) = "1"
dr_child2(3) = "Test.aspx"
childTB.Rows.Add(dr_child2)
Dim dr6 As DataRow = childTB.NewRow()
dr6(0) = "0"
dr6(1) = "T0201"
dr6(2) = "2"
dr6(3) = "Test.aspx"
childTB.Rows.Add(dr6)
Dim dr7 As DataRow = childTB.NewRow()
dr7(0) = "1"
dr7(1) = "T0202"
dr7(2) = "2"
dr7(3) = "Test.aspx"
childTB.Rows.Add(dr7)
' 使用DataSet来包含这两个表
Dim ds As New DataSet()
ds.Tables.Add(mainTB)
ds.Tables.Add(childTB)
' 建立主表和子表之间的关系
ds.Relations.Add("Child", ds.Tables(0).Columns("mainId"),
ds.Tables(1).Columns("MainId"))
Return ds
End Function
End Class
作者: binlie (20块)   2018-05-29 11:44:00
感觉直接组HTML比较简单
作者: chen5252 (锋炮炸裂)   2018-05-30 22:09:00
但你TABLE要再多开一个字段去对应母选单然后要再开一个母选单table,最后join select出来,跑一个循环就可以处理了,或是分两段捞出来,用你原本的方法

Links booklink

Contact Us: admin [ a t ] ucptt.com