如何使用ASP.NET菜单控件在水平线上显示菜单项?

huangapple go评论63阅读模式
英文:

How to display menu items in a horizontal line using ASP.NET Menu control?

问题

以下是您的代码的翻译部分:

我正在开发一个ASP.NET Web应用程序,并且正在使用菜单控件创建导航栏。然而,我在显示菜单项时遇到了一个问题,它们以垂直列表的形式显示。

我想要实现以下菜单布局:

Narudžbe | Artikli | Prijevoznici | Kupci | Izvještaji

这是我的代码:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="LandingPage.aspx.vb" Inherits="Mišković_OMIS_projekt.LandingPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        .navbar {
            background-color: #333;
            overflow: hidden;
            display: flex;
            justify-content: center; /* Align items horizontally */
        }

        .navbar a {
            float: none; /* Remove float property */
            color: #f2f2f2;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
            font-size: 20px;
        }

        .navbar a:hover {
            background-color: #ddd;
            color: black;
        }

        .logo {
            text-align: center;
            padding: 20px 0;
        }

        .logo img {
            width: 800px; /* Adjust the width as per your logo's dimensions */
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="navbar">
            <asp:Menu ID="MyMenu" runat="server">
                <Items>
                    <asp:MenuItem Text="Narudžbe" Value="~/Stranice/Narudzbe.aspx"></asp:MenuItem>
                    <asp:MenuItem Text="Artikli" Value="~/Stranice/Artikli.aspx"></asp:MenuItem>
                    <asp:MenuItem Text="Prijevoznici" Value="~/Stranice/Prijevoznici.aspx"></asp:MenuItem>
                    <asp:MenuItem Text="Kupci" Value="~/Stranice/Kupci.aspx"></asp:MenuItem>
                    <asp:MenuItem Text="Izvještaji" Value="~/Stranice/Izvjestaji.aspx"></asp:MenuItem>
                </Items>
            </asp:Menu>
        </div>
        <div class="logo">
            <img src="Images/logo.png" alt="OMIS Logo" />
        </div>
    </form>
</body>
</html>

我尝试修改了CSS属性,例如display: flexfloat: none,但菜单项仍然以垂直列表的方式显示。如何修改我的CSS或ASP.NET代码以实现所需的菜单项水平布局?

此外,这是VB.NET中的代码:

Public Class LandingPage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        MyMenu.Items(0).NavigateUrl = "~/Stranice/Narudzbe.aspx"
        MyMenu.Items(1).NavigateUrl = "~/Stranice/Artikli.aspx"
        MyMenu.Items(2).NavigateUrl = "~/Stranice/Prijevoznici.aspx"
        MyMenu.Items(3).NavigateUrl = "~/Stranice/Kupci.aspx"
        MyMenu.Items(4).NavigateUrl = "~/Stranice/Izvjestaji.aspx"
    End Sub

    Protected Sub MyMenu_MenuItemClick(sender As Object, e As MenuEventArgs) Handles MyMenu.MenuItemClick
        ' Get the selected item's URL
        Dim selectedItemUrl As String = MyMenu.SelectedItem.Value

        ' Redirect to the selected page
        Response.Redirect(selectedItemUrl)
    End Sub
End Class

希望这有所帮助。

英文:

I am working on an ASP.NET web application, and I'm using the Menu control to create a navigation bar. However, I'm facing an issue with displaying the menu items in a horizontal line. Currently, the menu items are displayed vertically like a list.

I want to achieve the following layout for my menu:

Narudžbe | Artikli | Prijevoznici | Kupci | Izvještaji

Here is my code:

&lt;%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; CodeBehind=&quot;LandingPage.aspx.vb&quot; Inherits=&quot;Mišković_OMIS_projekt.LandingPage&quot; %&gt;
&lt;!DOCTYPE html&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head runat=&quot;server&quot;&gt;
&lt;title&gt;&lt;/title&gt;
&lt;style&gt;
.navbar {
background-color: #333;
overflow: hidden;
display: flex;
justify-content: center; /* Align items horizontally */
}
.navbar a {
float: none; /* Remove float property */
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 20px;
}
.navbar a:hover {
background-color: #ddd;
color: black;
}
.logo {
text-align: center;
padding: 20px 0;
}
.logo img {
width: 800px; /* Adjust the width as per your logo&#39;s dimensions */
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
&lt;div class=&quot;navbar&quot;&gt;
&lt;asp:Menu ID=&quot;MyMenu&quot; runat=&quot;server&quot;&gt;
&lt;Items&gt;
&lt;asp:MenuItem Text=&quot;Narudžbe&quot; Value=&quot;~/Stranice/Narudzbe.aspx&quot;&gt;&lt;/asp:MenuItem&gt;
&lt;asp:MenuItem Text=&quot;Artikli&quot; Value=&quot;~/Stranice/Artikli.aspx&quot;&gt;&lt;/asp:MenuItem&gt;
&lt;asp:MenuItem Text=&quot;Prijevoznici&quot; Value=&quot;~/Stranice/Prijevoznici.aspx&quot;&gt;&lt;/asp:MenuItem&gt;
&lt;asp:MenuItem Text=&quot;Kupci&quot; Value=&quot;~/Stranice/Kupci.aspx&quot;&gt;&lt;/asp:MenuItem&gt;
&lt;asp:MenuItem Text=&quot;Izvještaji&quot; Value=&quot;~/Stranice/Izvjestaji.aspx&quot;&gt;&lt;/asp:MenuItem&gt;
&lt;/Items&gt;
&lt;/asp:Menu&gt;
&lt;/div&gt;
&lt;div class=&quot;logo&quot;&gt;
&lt;img src=&quot;Images/logo.png&quot; alt=&quot;OMIS Logo&quot; /&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

I have tried modifying the CSS properties such as display: flex and float: none, but the menu items are still displayed in a vertical list. How can I modify my CSS or ASP.NET code to achieve the desired horizontal layout for the menu items?

Here is also the code-behind in VB.NET:

Public Class LandingPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
MyMenu.Items(0).NavigateUrl = &quot;~/Stranice/Narudzbe.aspx&quot;
MyMenu.Items(1).NavigateUrl = &quot;~/Stranice/Artikli.aspx&quot;
MyMenu.Items(2).NavigateUrl = &quot;~/Stranice/Prijevoznici.aspx&quot;
MyMenu.Items(3).NavigateUrl = &quot;~/Stranice/Kupci.aspx&quot;
MyMenu.Items(4).NavigateUrl = &quot;~/Stranice/Izvjestaji.aspx&quot;
End Sub
Protected Sub MyMenu_MenuItemClick(sender As Object, e As MenuEventArgs) Handles MyMenu.MenuItemClick
&#39; Get the selected item&#39;s URL
Dim selectedItemUrl As String = MyMenu.SelectedItem.Value
&#39; Redirect to the selected page
Response.Redirect(selectedItemUrl)
End Sub
End Class

答案1

得分: 0

Here is the translation of the provided code:

尝试这个菜单设置:

如何使用ASP.NET菜单控件在水平线上显示菜单项?

如果您在属性表中设置了上述内容,那么您的菜单将变成这样:

        &lt;asp:Menu ID=&quot;MyMenu&quot; runat=&quot;server&quot; 
Orientation=&quot;Horizontal&quot; RenderingMode=&quot;Table&quot;&gt;
&lt;Items&gt;

结果如下:

如何使用ASP.NET菜单控件在水平线上显示菜单项?

英文:

Try this for the menu settings:

如何使用ASP.NET菜单控件在水平线上显示菜单项?

if you set above in property sheet, then your menu becomes this:

        &lt;asp:Menu ID=&quot;MyMenu&quot; runat=&quot;server&quot; 
Orientation=&quot;Horizontal&quot; RenderingMode=&quot;Table&quot;&gt;
&lt;Items&gt;

And the result is this:

如何使用ASP.NET菜单控件在水平线上显示菜单项?

huangapple
  • 本文由 发表于 2023年5月14日 17:04:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定