You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET WebForms中Bootstrap 5.2.3导航栏下拉菜单无法展开求助

Bootstrap 5下拉菜单无法展开的修复方案(.NET WebForms)

问题诊断

你的代码里存在几个导致下拉菜单失效的核心问题:

  • 版本不匹配:NuGet安装的是Bootstrap 5.2.3,但页面引入了5.1.1的CDN脚本,同时重复加载本地Bootstrap脚本,引发版本冲突
  • 属性错误:Bootstrap 5要求使用data-bs-toggle触发下拉,你仍在使用旧版的data-toggle,组件无法识别交互指令
  • 资源重复引入:同时通过Bundle和单独链接加载Bootstrap CSS,可能导致样式冲突或加载异常

修复步骤

  1. 统一Bootstrap资源
    删除页面头部的CDN脚本链接,保留NuGet引入的本地资源,确保CSS和JS版本统一为5.2.3;移除重复的CSS引用,避免冲突。

  2. 修正下拉交互属性
    将下拉按钮的data-toggle替换为data-bs-toggle,确保Bootstrap 5能正确识别触发逻辑。

  3. 优化组件结构
    调整下拉菜单内的链接结构,符合Bootstrap 5的规范(直接将nav-link替换为dropdown-item,无需嵌套在<li class="dropdown-item">内)。

  4. 确保依赖完整
    使用bootstrap.bundle.min.js代替bootstrap.js,该版本已包含Popper.js(Bootstrap 5交互组件必需的依赖)。

修复后的完整代码

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="EmployeeManagementSystem.SiteMaster" %>

<!DOCTYPE html>
<html lang="en">
<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%: Page.Title %> - My ASP.NET Application</title>

    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/modernizr") %>
    </asp:PlaceHolder>

    <webopt:BundleReference runat="server" Path="~/Content/css" />
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <link rel="stylesheet" href="https://kit.fontawesome.com/c8cac405d2.css" crossorigin="anonymous">
</head>
<body>
    <form runat="server">
        <asp:ScriptManager runat="server">
            <Scripts>
                <%--Framework Scripts--%>
                <asp:ScriptReference Name="MsAjaxBundle" />
                <asp:ScriptReference Name="jquery" />
                <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                <asp:ScriptReference Name="WebFormsBundle" />
                <%--Site Scripts--%>
            </Scripts>
        </asp:ScriptManager>

        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark">
            <div class="container">
                <a class="navbar-brand" runat="server" href="~/">Employee Management System</a>
                <button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" title="Toggle navigation" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse d-sm-inline-flex justify-content-between">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item"><a class="nav-link" runat="server" href="~/">Home</a></li>

                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="basicFormsDropdown" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Basic Forms
                            </a>

                            <ul class="dropdown-menu" aria-labelledby="basicFormsDropdown">
                                <li><a class="dropdown-item" runat="server" href="~/Pages/Employee">Employee</a></li>
                                <li><a class="dropdown-item" runat="server" href="~/Pages/Department">Department</a></li>
                                <li><a class="dropdown-item" runat="server" href="~/Pages/Address">Address</a></li>
                                <li><a class="dropdown-item" runat="server" href="~/Pages/Role">Role</a></li>
                            </ul>
                        </li>
                        <li class="nav-item"><a class="nav-link" runat="server" href="~/Pages/Job">Job</a></li>
                        <li class="nav-item"><a class="nav-link" runat="server" href="~/Pages/JobHistory">Job History</a></li>
                        <li class="nav-item"><a class="nav-link" runat="server" href="~/Pages/VotingRecord">Voting Record</a></li>
                        <li class="nav-item"><a class="nav-link" runat="server" href="~/Pages/EOM">EOM</a></li>
                    </ul>
                </div>
            </div>
        </nav>

        <div class="container body-content">
            <asp:ContentPlaceHolder ID="MainContent" runat="server">
            </asp:ContentPlaceHolder>
            <hr />
            <footer>
                <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
            </footer>
        </div>
    </form>
    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/Scripts/bootstrap.bundle.min.js") %>
    </asp:PlaceHolder>
</body>
</html>

内容的提问来源于stack exchange,提问作者SDhakZZZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 01:29:54