ASP.NET MasterPage菜单栏

时间:2014-05-08 06:56:17

标签: asp.net master-pages menubar

我有MasterPage和4页。

<div class="navcover">
    <div class="nav"style="background-image: url(images/lion.jpg);">
        <a href="Default.aspx">Home</a>
    </div>
    <div class="nav"> 
         <a href="Services.aspx">Services</a> 
    </div>
    <div class="nav">
         <a href="careers.aspx">Careers</a>
    </div>
    <div class="nav"style="background-image: url(images/lion.jpg);">
        <a href="contactus.aspx">Contact Us</a>
    </div>
</div>

当我去服务时,背景图片必须更改为images / lion.jpg,当我去职业等时...... 我该如何设置这些更改?

2 个答案:

答案 0 :(得分:0)

您需要在母版页上找到所请求的页面名称。

protected void Page_Load(object sender, EventArgs e)
{
    if (this.MainContent.Page is Services)
    {
        // The default page
    }
}

制作商品runat='server',以便您可以在母版页上获得该商品的参考。 然后使用.attributs函数相应地更改属性(背景)。

<div class="nav"style="background-image: url(images/lion.jpg);" runat="Server">


protected void Page_Load(object sender, EventArgs e)
{
    if (this.MainContent.Page is Services)
    {
       //get reference of div and add style

答案 1 :(得分:0)

试试这个

<div class="navcover">
            <div class="nav" id="nav1" style="background-image: url(images/lion.jpg);">
                <a href="Default.aspx">Home</a>
            </div>
            <div class="nav" id="nav2"> 
                 <a href="Services.aspx">Services</a> 
            </div>
            <div class="nav" id="nav3">
                 <a href="careers.aspx">Careers</a>
            </div>
            <div class="nav" id="nav2" style="background-image: url(images/lion.jpg);">
                <a href="contactus.aspx">Contact Us</a>
            </div>
        </div>
    <div>

并在母版页中添加此javascript函数

<script>
   window.onload = function()
    {
        var url = document.URL;
        if(url.indexOf('Default2.aspx') != -1)
        {
            var a = document.getElementById("nav1");
            a.style.backgroundImage="url('images/lion.jpg')";
        }    
        if(url.indexOf('Services.aspx') != -1)
        {
            var a = document.getElementById("nav2");
            a.style.backgroundImage="url('images/lion.jpg')";
        }  
        if(url.indexOf('careers.aspx') != -1)
        {
            var a = document.getElementById("nav3");
            a.style.backgroundImage="url('images/lion.jpg')";
        }  
        if(url.indexOf('contactus.aspx') != -1)
        {
            var a = document.getElementById("nav4");
            a.style.backgroundImage="url('images/lion.jpg')";
        }  
    };
    </script>