2016年3月15日 星期二

ASP.NET 使用資料庫動態產生功能表(Menu)

網頁內容如下:

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="css/jquery.mcdropdown.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <style type="text/css">
        .menu
        {
            width: 913px;
            font-family: verdana, Segoe UI;
            margin: 0 auto;
            border: 1px solid #B34C00;
            border-radius: 4px;
        }
        .menu ul
        {
            padding: 10px;
            background-color: #FF6600;
            float: left;
            margin: 0px;
            list-style: none;
        }
        .menu ul li        {
            display
inline-block;
            float: left;
            position: relative;
            cursor: pointer;
        }
        .menu ul li a        {
            cursor:
ointer;
            display: block;
            padding: 10px;
            float: left;
            color: #fff;
            text-decoration: none;
        }
        .menu ul li ul
        {
            display: none;
            margin-top: 10px;
        }
        .menu ul li:hover ul
        {
            display: block;
            width: 200px;
            position: absolute;
            left: 0px;
            top: 25px;
            background: #FF6600;
            border: 1px solid #B34C00;
            border-top: none;
            color: #fff;
        }
        .menu ul li:hover ul li
        {
            padding: 5px;
            float: none;
            display: block;
        }
        .menu ul li:hover ul li a
        {
            padding: 5px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="menu">
        <asp:Panel ID="Panel1" runat="server" Width="913px" Style="margin: 0px">
        </asp:Panel>
    </div>
    </form>
    <p>
    </p>
</body>
</html>

程式內容如下:


Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Imports System.Data

Public Partial Class _Default
    Inherits System.Web.UI.Page
    Private con As New SqlConnection("Data Source=(local);Initial Catalog=master;uid=sa; pwd=Micr0s0ft")
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        Dim da As New SqlDataAdapter("Select TechnologyID, TechnologyName, TechnologyURL from Technology", con)
        Dim dttc As New DataTable()
        da.Fill(dttc)
        Dim main As HtmlGenericControl = UList("Menuid", "menu")
        For Each row As DataRow In dttc.Rows
            da = New SqlDataAdapter("select TechnologyCategoryID,TechnologyCategoryName,TechnologyCategoryURL from TechnologyCategory where TechnologyID=" + row("TechnologyID").ToString(), con)
            Dim dtDist As New DataTable()
            da.Fill(dtDist)
            If dtDist.Rows.Count > 0 Then
                Dim sub_menu As HtmlGenericControl = LIList(row("TechnologyName").ToString(), row("TechnologyID").ToString(), row("TechnologyURL").ToString())
                Dim ul As New HtmlGenericControl("ul")
                For Each r As DataRow In dtDist.Rows
                    ul.Controls.Add(LIList(r("TechnologyCategoryName").ToString(), r("TechnologyCategoryID").ToString(), r("TechnologyCategoryURL").ToString()))
                Next
                sub_menu.Controls.Add(ul)
                main.Controls.Add(sub_menu)
            Else
                main.Controls.Add(LIList(row("TechnologyName").ToString(), row("TechnologyID").ToString(), row("TechnologyURL").ToString()))
            End If
        Next
        Panel1.Controls.Add(main)
    End Sub
    Private Function UList(id As String, cssClass As String) As HtmlGenericControl
        Dim ul As New HtmlGenericControl("ul")
        ul.ID = id
        ul.Attributes.Add("class", cssClass)
        Return ul
    End Function
    Private Function LIList(innerHtml As String, rel As String, url As String) As HtmlGenericControl
        Dim li As New HtmlGenericControl("li")
        li.Attributes.Add("rel", rel)
        li.InnerHtml = (Convert.ToString("<a href=" + String.Format("
http://{0}", url) + ">") & innerHtml) + "</a>"
        Return li
    End Function
End Class

沒有留言: