I have a unordered list in my master page....
<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>
From a content page i am assigning a css class to one of the list item...
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);
}
}
and my 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;开发者_C百科
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;
}
But i get the error,
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?
I am trying to assign current
to Manage
li from my content page to my master page... Any suggestion...
Like it says, there's no ContainsKey
method in AttributeCollection
.
Change your code to the following and it'll do the same thing:
string classAttribute = home.Attributes["class"];
if (string.IsNullOrEmpty(classAttribute))
{
home.Attributes.Add("class", cssToApply);
}
else
{
if (!classAttribute.Contains(cssToApply))
{
home.Attributes["class"] += " " + cssToApply;
}
}
I once had a similar issue though I think it is much more simple than what both of you are proposing here. To apply a css object to a server contol from a control to a Master Page, you drop this into each of the pages
in the control page add
MasterPageFile="~/MyMasterPage.master"
in the code behind files of your controls
using System.WEB.UI.Htmlcontrols;//add your namespace//
HtmlGenericControls mycontrol = (HtmlGenericControl)this.Page.Master.FindControl("yourcontrolname") as HtmlGenericControl;
mycontrol.Attributes.Add("class", "cssToApply");
It does not store the current pages added class it is destroyed once you leave the page and visit another so you dont need to worry about creating repetition
<div class"X" class"X" class"X">
In this scenario "mycontrol" applies to a navigation menu of list items where I wanted the current pages navigation item to highlight while on the page. This can also apply not only to CSS class but also CSS id. This technique can be applied without the use of importing or making any major changes to inherit the master page file.
I hope this helps and I now have a resource out there to help me when I forget how to do this haha.
精彩评论