2010-07-21 61 views
3

從contentpage分配CSS類的母版控制我有我的母版頁中的無序列表....在asp.net

<ul id="mainMenu" runat="server"> 
<li id="mainHome" runat="server"><a href="#" title="Home" class="home"> 
     <span></span>Home</a></li> 
<li id="mainManage" runat="server"><a href="Users.aspx" title="Manage" 
    class="manage"><span></span>Manage</a></li> 
<li id="mainEnquiry" runat="server"><a href="account.aspx" title="Enquiry" 
    class="enquiry"><span></span>Enquiry</a></li> 
<li id="mainReport" runat="server"><a href="EnquiryReport.aspx" title="Report" 
     class="report"><span></span>Report</a></li> 
    </ul> 

從內容頁我正在分配一個CSS類的一個列表項...

HtmlGenericControl home = (HtmlGenericControl)this.Page.Master.FindControl("mainMenu").FindControl("mainManage") as HtmlGenericControl; 
       string cssToApply = "current"; 

       if (null != home) 
       { 
        if (home.Attributes.ContainsKey("class")) 
        { 
         if (!home.Attributes["class"].Contains(cssToApply)) 
         { 
          home.Attributes["class"] += " " + cssToApply; 
         } 
        } 
        else 
        { 
         home.Attributes.Add("class", cssToApply); 
        } 
       } 

和我的CSS,

#header ul li { 
display:inline; 
float:left; 
} 
#header ul a { 
-x-system-font:none; 
color:#FFFFFF; 
display:block; 
font-family:Trebuchet MS,Arial,sans-serif; 
font-size:1.1em; 
font-style:normal; 
font-variant:normal; 
font-weight:bold; 
text-transform:uppercase; 
text-decoration:none; 
} 
#header ul a { 
-moz-border-radius:3px; 
-webkit-border-radius:0.2em; 
padding:3px 7px; 
text-decoration:none; 
} 
#header ul a:focus, #header ul a:active, #header ul a:hover { 
background-color:#829E7E; 
outline-color:-moz-use-text-color; 
outline-style:none; 
outline-width:medium; 
} 
#header ul a.home { 
margin:0 16px 0 17px; 
} 
#header ul #current a, #headermenu #current span{ /*currently selected tab*/ 
background-color: #BCE27F; 
color:#666666; 
white-space:nowrap; 
} 
#header ul a.manage,#header ul a.enquiry,#header ul a.report { 
margin:0 14px 0 0; 
} 
#home #header ul a.home, #enquiry #header ul a.enquiry, #report #header ul a.report, #manage #header ul a.manage{ 
-moz-border-radius:3px; 
-webkit-border-radius:0.2em; 
background-color:#B9E27F; 
color:#FFFFFF; 
} 

,但我得到的錯誤,

System.Web.UI.AttributeCollection' does not contain a definition for 'ContainsKey' and no extension method 'ContainsKey' accepting a first argument of type 'System.Web.UI.AttributeCollection' could be found (are you missing a using directive or an assembly reference?

我想從我的內容頁面分配currentManage裏我的母版頁...任何建議...

+0

主頁和內容頁面的非常好的問題。 – 2013-04-24 13:07:35

回答

4

像它說,有一個在AttributeCollection沒有ContainsKey方法。

你的代碼更改爲以下,它會做同樣的事情:

string classAttribute = home.Attributes["class"]; 
if (string.IsNullOrEmpty(classAttribute)) 
{ 
    home.Attributes.Add("class", cssToApply); 
} 
else 
{ 
    if (!classAttribute.Contains(cssToApply)) 
    { 
     home.Attributes["class"] += " " + cssToApply; 
    } 
} 
1

我曾經有過一個類似的問題,但我認爲這是比你們兩個提議在這裏要簡單得多。應用CSS對象從控制到母版頁的服務器控逆變,你下降到每一個頁面的這個

在控制頁面

在代碼中添加

MasterPageFile="~/MyMasterPage.master" 

您的控件

的隱藏文件
using System.WEB.UI.Htmlcontrols;//add your namespace// 

HtmlGenericControls mycontrol = (HtmlGenericControl)this.Page.Master.FindControl("yourcontrolname") as HtmlGenericControl; 

mycontrol.Attributes.Add("class", "cssToApply"); 

它不保存當前頁面,一旦你離開這個頁面被摧毀添加類和訪問另一個所以你不需要擔心創建重複

<div class"X" class"X" class"X"> 

在這種情況下,「mycontrol」適用於列表項的導航菜單,我希望當前頁面導航項在頁面上突出顯示。這不僅適用於CSS類,還適用於CSS id。無需使用導入或進行任何重大更改來繼承母版頁文件,即可應用此技術。

我希望這可以幫助,我現在有一個資源在那裏幫助我,當我忘記如何做到這一點哈哈。