2013-07-01 78 views
0

可以爲內容佔位符設置背景圖像嗎?目前,我可以顯示整個頁面的背景圖片,但內容佔位符阻止了大部分圖片,所以我想爲內容佔位符設置背景圖像(大部分內容位於持有者中) 。內容佔位符背景圖像

這裏是我的代碼,以顯示在背景的圖像,但我還是不知道如何將其放置在內容佔位符在ASP.NET:

protected void dropListActivity_SelectedIndexChanged(object sender, EventArgs e) 
    { 

     if (dropListActivity.SelectedItem.Text == "GrassHopper and Ants") 
     { 
      PageBody.Attributes.Add("style", "background:url(Images/background/ant-and-grasshopper.jpg) no-repeat;"); 
      Session["Background"] = "background:url(Images/background/ant-and-grasshopper.jpg);"; 

     } 

     if (dropListActivity.SelectedItem.Text == "Food Fit For A King") 
     { 
      PageBody.Attributes.Add("style", "background:url(Images/background/King.jpg) no-repeat;"); 
      Session["Background"] = "background:url(Images/background/King.jpg);"; 

     } 
    } 

,並在我的HTML身邊,我只加身id =「PageBody」,它完成了這項工作。但是我如何在內容持有者中做到這一點?我是編程方面的新手,以及CSS或Html。

回答

0

內容佔位符只呈現它的內容,沒有html元素在渲染時呈現,如果它是空的,你可以在它內部放置一個div,它會渲染。

而關於你的解決方案 - 你要去服務器更改背景圖片,請不要。
您應該在JavaScript + CSS中使用它 使用jQuery來偵聽select元素的更改事件,然後根據它更改背景。

in your css: 
.grassHopper{background:url(Images/background/ant-and-grasshopper.jpg) no-repeat;} 
.foodFit{background:url(Images/background/King.jpg) no-repeat;} 
in your JS 
$(document).ready(function(){ 
    $("#dropListActivity").change(function(event){ 
     var jqBody = $("body"); 
     jqBody.removeClass("grassHopper foodFit"); 
     if($(this).find("option[selected]").text() === "GrassHopper and Ants") 
     { 
     jqBody.addClass("grassHopper"); 
     } 
     ... 
    }).change();//also trigger so the bg will be updated according to the selected value at the select from the server 
}); 

而最後一件事 - 如果你只使用.NET堆棧上的編程開始 - 我建議期運用asp.net MVC和ONT的WebForms