Creating a dynamic hover drop-down navigation bar in ASP.NET

After successfully creating a navigation bar with hover effects, I am now looking to implement a dropdown list when hovering over the items. Can anyone provide guidance on how to achieve this? Below is the current code snippet I am working with:


<ul class="nav nav-pills nav-justified">
    <li class="auto-style1"><a runat="server" href="~/" class="auto-style2"><strong><span class="auto-style3">Home Page</span></strong></a></li>
    <li class="auto-style1"><a runat="server" href="~/About" class="auto-style2"><strong><span class="auto-style3">Painting</span></strong></a></li>
    <li class="auto-style1"><a runat="server" href="~/Contact" class="auto-style2"><strong><span class="auto-style3">Drawing</span></strong></a></li>
    <li class="auto-style1"><a runat="server" href="~/" class="auto-style2"><strong><span class="auto-style3">Surfaces</span></strong></a></li>
    <li class="auto-style1"><a runat="server" href="~/About" class="auto-style2"><strong><span class="auto-style3">Easels</span></strong></a></li>
    <li class="auto-style1"><a runat="server" href="~/Contact" class="auto-style2"><strong<span class="auto-style3">Display</span></strong></a></li>
</ul>

Additionally, here is a screenshot of the outcome after running the code:

https://example.com/screenshot123

Answer №1

Update: For a more detailed explanation on where the following should be placed, particularly in reference to webforms, it is recommended to access the Site.Master page.

Incorporating into Site.Master Page: Assuming you have retained the default code generated by Visual Studio, you should locate the following lines within the <Scripts></Scripts> tags:

        <asp:ScriptReference Name="MsAjaxBundle" />
        <asp:ScriptReference Name="jquery" /> 
        <asp:ScriptReference Name="bootstrap" />
        <asp:ScriptReference Name="respond" />
        <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" />

This indicates that jQuery and Bootstrap are being imported into the pages. JQuery serves as a JavaScript library facilitating ease of development, while Bootstrap enhances styling with minimal effort through predefined classes.

The previously mentioned code snippet contains the following additional part by default:

  <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" runat="server" href="~/">Application name</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a runat="server" href="~/">Home</a></li>
                    <li><a runat="server" href="~/About">About</a></li>
                    <li><a runat="server" href="~/Contact">Contact</a></li>
                </ul>
            </div>
        </div>
    </div>

You might find this familiar as it closely resembles your original post's markup structure. To include a drop-down list, you can refer to the below code highlighting a dropdown feature and incorporate required elements into the provided code block:

    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" runat="server" href="~/">Application name</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a runat="server" href="~/">Home</a></li>
                    <li><a runat="server" href="~/About">About</a></li>
                    <li><a runat="server" href="~/Contact">Contact</a></li>
                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 4
                        <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                          <li><a runat="server" href="#">Page 1-1</a></li>
                          <li><a runat="server" href="#">Page 1-2</a></li>
                          <li><a runat="server" href="#">Page 1-3</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </div>

By implementing this, your navbar along with the dropdown functionality should respond accurately upon user interaction.

Incorporating jQuery: It's advisable to load any external scripts, especially larger ones, towards the end for optimized performance. This ensures the page does not wait long for loading content to provide users with a seamless experience. Though ASP.NET MVC handles this aspect better than Webforms, adherence to best practices is beneficial.

Returning to the Site.Master page, proceed to its conclusion right before the closing `' tag. Place the following script snippet:

<script>
    $(document).ready(function () {
        $(".dropdown").hover(function () {
            //toggle the open class (on/off)
            $(this).toggleClass("open");
        });
    })
</script>

To ensure correct execution, the final layout should resemble...:

... ... ...
... ... ...
        <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>
    <script>
        //this line means we won't do anything until the page is ready for it
        $(document).ready(function () { 
            $(".dropdown").hover(function () {
                //toggle the open class (on/off)
                $(this).toggleClass("open");
            });
        })
    </script>
</body>
</html>

Prior to the Update:

Note: Assuming usage of bootstrap and jQuery due to ASP.NET tagging and resemblance of class names to bootstrap syntax.

A simplified method utilizing bootstrap and jQuery includes the following approach:

jsfiddle: https://jsfiddle.net/tzv3t12c/9/

Bootstrap Nav Template:

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">WebSiteName</a>
    </div>
    <ul class="nav navbar-nav">
      <li class="active"><a href="#">Home</a></li>
      <!-- The li element requiring manual adjustment on hover.
           Simulating the bootstrap event triggered when the link is clicked.
           While it may lack full functionality compared to bootstrap's click event,
           it provides quick implementation for interim requirements.
      -->
      <li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1
        <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a href="#">Page 1-1</a></li>
          <li><a href="#">Page 1-2</a></li>
          <li><a href="#">Page 1-3</a></li>
        </ul>
      </li>
      <li><a href="#">Page 2</a></li>
      <li><a href="#">Page 3</a></li>

      <li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 4
        <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a href="#">Page 1-1</a></li>
          <li><a href="#">Page 1-2</a></li>
          <li><a href="#">Page 1-3</a></li>
        </ul>
      </li>
    </ul>
  </div>
</nav>

Essential jQuery Functionality:

//attach hover event to elements with dropdown class
$(".dropdown").hover(function(){
  //toggle the open class (on/off)
  $(this).toggleClass("open");
});

Rationale:

The objective here is to replicate bootstrap's behavior upon activating dropdown links via simulated clicks. By observing the HTML alterations using developer tools such as Chrome's console (F12), it becomes evident that the addition of the open class to the li marked as dropdown signifies transition activation. Other related modifications during the actual bootstrap click action should also be considered for encompassing complete simulation beyond mere class toggling.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Embedding an iframe with vertical scrolling enabled

Could you explain why I am unable to scroll the full width of the website? Here is a link: http://codepen.io/anon/pen/EKPaao?editors=1100. Also, what do you think about using this solution for adjusting iframes on different screen sizes? My iframe seems to ...

My goal is to develop a PHP photo gallery using either an array, session variables, or cookies

I am in need of a solution for my issue. https://i.stack.imgur.com/Xe65l.png The image shows the front end, and I require a corresponding function to be developed for it. ...

"Keep a new tab open at all times, even when submitting a form in

I have a form with two submit buttons - one to open the page in a new tab (preview) and another for regular form submission (publish). The issues I am facing are: When I click the preview button to open in a new tab, if I then click the publish button a ...

Clicking on React Js reverses an array that had previously been unreversed

My issue involves an array pulled from a database that I have reversed so that the newest entry is displayed at the top when a button is clicked, opening a modal-style window. However, when the array is displayed in the modal window, it appears to be flipp ...

Adjusting the background color using CSS according to the browser's dimensions

I'm having trouble identifying the issue with this code. The background color remains white and doesn't change as expected. <style type="text/css"> h1 { position: absolute; text-align: center; width: 100%; ...

In both Chrome and Edge, the default value for the <select> tag is successfully set, however, this functionality is not working in

I have defined two values in the created method, 2018 and My Name, and assigned them to separate data properties. These data properties are then passed as v-bind to a component. The issue I am facing is that in Chrome and Edge, both values are set as defa ...

Warning: The username index is not defined in the file C:xampphtdocsindex.php at line 4

Hey there, I just wanted to express my gratitude for taking the time to read this. It's all part of a college assignment (web server scripting unit p4) where I am working on setting up a simple login system. Unfortunately, I keep running into an error ...

Tips on adding CSS to a React component

I've successfully converted an HTML file into a component designed to resemble a 7-segment LED display. I have imported the necessary CSS styles from the index.css file, but I am unsure how to properly apply these styles within the component. My atte ...

Is it possible to expand the Client table in Identity Server 4 by including additional columns?

Currently working on implementing Identity server 4 and need to include an additional column in the Client table. I'm wondering if it's possible to extend the Client class to add a column to the table? ...

How can I use an HTML button to activate a function that inserts text into a read-only text-box?

Trying to write a simple piece of HTML code that finds the number greater than or equal to the first initial amount that wholly divides the second given amount. The code attempts to divide the numbers, and if it fails, increments the first number by 1 and ...

Error: The function window.intlTelInput is not recognized within the ReactJS framework

I am currently learning ReactJS and encountering an issue when using jQuery with React JS for intlTelInput. I have installed npm jQuery and imported all the necessary code. Additionally, I have included all the required CSS and jQuery links in my index.htm ...

Randomly positioning an image while the page is loading

I've been developing a portfolio website to showcase my design work, but I've encountered a minor issue. My goal is to have the images load in random positions and then make them draggable using Dragabilly. However, sometimes all the images end ...

A guide on transforming JSON data into HTML using Rails

I'm struggling with parsing JSON on a webpage. My webpage is designed with circles, where clicking on a circle triggers a call to the controller to retrieve specific information from a database and display it as graphs and text. The issue I'm fa ...

What are the steps for implementing JWT authentication with static content hosting?

My website consists of a frontend (built with Vue) and a backend (developed using node). The api relies on jwt authentication, allowing only logged-in users to interact with the backend. The jwt is included in the Authorization: Bearer xxx header. An iss ...

Is it possible to redirect any web traffic using an authentication script?

Scenario: I'm currently working on a social networking project and I'm looking for a way to validate every redirect or refresh by directing the user through another script before reaching their final destination. I've considered using a &apo ...

Arrange the footer content into multiple columns

Hello! I'm currently working on creating a footer for my website. I've written this code and everything seems to be functioning correctly, except for the fact that the second row is positioned under the first row and taking up half of the bottom ...

Troubleshooting Image Map Failure on Internet Explorer 10

<img src="images/imagemap.png" width="600" height="100" border="0" usemap="#map" /> <map name="map"> <!-- #$-:Image map file created by GIMP Image Map plug-in --> <!-- #$-:GIMP Image Map plug-in by Maurits Rijk --> <!-- #$-:Plea ...

Design of website built on Bootstrap framework seems distorted on Firefox browser

I built this website with the help of Bootstrap and you can view it at js-projects. Everything looks great when I open it in Chrome or Safari, but there are weird white strips that resemble scroll bars when I view it in Firefox. Can someone assist me in ...

How can I restrict the selection of only one checkbox within an iframe window?

Here is my JavaScript snippet: var iframe = document.getElementById('pltc'); iframe.contentWindow.document.open('text/htmlreplace'); iframe.contentWindow.document.write('<input type="checkbox" name="tc0">Yes<input type="c ...

SVGs do not display on iPhones

Having some issues with my code - specifically, I have an SVG animation that seems to work on all devices except iPhones. I've been struggling to find a solution. Here is the code snippet for the SVG: <div class="contenuto-svg"> <svg vi ...