ASP.Net在主页上隐藏左手菜单

时间:2015-08-04 07:46:35

标签: html asp.net

我有一个asp.net网站,其中添加了一个左手菜单并且遇到了问题,因为我发现我正在进行大量的代码重复(我知道这很糟糕)。我想要的是将左侧菜单添加到我的Site.Master文件而不是我必须将其添加到每个页面。

我可以这样做,但左手菜单会显示在" Home"我不想要的页面,所以我认为我需要某种IF声明,但我不知道如何做到这一点,因为当用户点击时我无法查看URL页面是标准命名(例如http://www.webaddress.co.uk/

我的左手菜单是一个滚动的nav-stacked工作正常,所以我还需要将这个JQuery添加到Site.Master中。

我的'关于我们的当前HTML'页

<asp:Content ID="AboutBodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <div class="col-sm-3 hidden-xs" style="padding-left: 0px">
        <div class="follow-scroll">
            <ul class="nav nav-stacked">
                <li><a runat="server" href="~/">Home</a></li>
                <li class="active"><a runat="server" href="~/About">About</a></li>
                <li><a runat="server" href="~/Session/pg1">Session</a></li>
                <li><a runat="server" href="~/EmailPg">Email</a></li>
            </ul>
        </div>
    </div>

    <div class="col-xs-12 col-sm-9">
        <h2><%: Title %>.</h2>
        <p>Use this area to provide additional information.</p>
        <p>Use this area to provide additional information.</p>
        <p>Use this area to provide additional information.</p>
    </div>

    <script type="text/javascript">
        (function ($) {
            var element = $('.follow-scroll'),
                originalY = element.offset().top;

            // Space between element and top of screen (when scrolling)
            var topMargin = 75;

            // Should probably be set in CSS; but here just for emphasis
            element.css('position', 'relative');

            $(window).on('scroll', function (event) {
                var scrollTop = $(window).scrollTop();

                element.stop(false, false).animate({
                    top: scrollTop < originalY
                            ? 0
                            : scrollTop - originalY + topMargin
                }, 300);
            });
        })(jQuery);
    </script>
</asp:Content>

以下HTML代码是我对Site.Master的想法,但正如我所说,它不需要显示在&#34;主页上。

    <div class="container body-content" style="padding-top: 25px">
        <div class="container">
            <div class="row">
                <div class="col-xs-12">                            
                    <div class="col-sm-3 hidden-xs" style="padding-left: 0px">
                        <!-- 'IF' statement to go here so not displayed on Home page -->
                        <ul class="nav nav-stacked">
                            <li><a runat="server" href="~/">Home</a></li>
                            <li style="border-left: 1px solid lightgray"><a runat="server" href="~/About">About</a></li>
                            <li style="border-left: 1px solid lightgray"><a runat="server" href="~/Session/pg1">Session</a></li>
                            <li style="border-left: 1px solid lightgray"><a runat="server" href="~/EmailPg">Email</a></li>
                        </ul>
                    </div>
                    <div class="col-xs-12 col-sm-9" style="padding-right: 0px">
                        <asp:ContentPlaceHolder ID="MainContent" runat="server"></asp:ContentPlaceHolder>
                    </div>
                </div>
            </div>                    
        </div>
    </div>

1 个答案:

答案 0 :(得分:2)

要解决所有问题,我在菜单的容器中添加了IDrunat="server",然后我可以在后面的代码中访问它:

div class="follow-scroll">
     <div ID="Sidebar" runat="server" class="col-sm-3 hidden-xs" style="padding-left: 0px">
          <ul class="nav nav-stacked">
               <li><a runat="server" href="~/">Home</a></li>
               <li><a runat="server" href="~/About">About</a></li>
               <li><a runat="server" href="~/Session/pg1">Session</a></li>
               <li><a runat="server" href="~/EmailPg">Email</a></li>
          </ul>
     </div>
</div>

然后,在母版页的代码隐藏中,我可以执行以下操作:

C#:

protected void Page_Load(object sender, EventArgs e)
{
    var URL = Request.Url.PathAndQuery;
    if(URL == "/default" || URL == "/default.aspx")
    {
        this.Sidebar.Visible = false;
    }
}

这使用HttpContext.Request.Url属性中的PathAndQuery,这是Uri个对象。

然后我能够将我的JQuery移动到我的site.master文件