在asp.net contentpage分配CSS类为母版控制 [英] assigning css class to a masterpage control from contentpage in asp.net
本文介绍了在asp.net contentpage分配CSS类为母版控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个无序列表在我的母版页....
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>
从内容页我正在指定一个CSS类的列表项之一...
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);
}
}
和我的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;
}
但我得到的错误,
But i get the error,
System.Web.UI.AttributeCollection'不包含定义的containsKey和没有扩展方法的containsKey接受式的第一个参数System.Web.UI.AttributeCollection可能是(是否缺少using指令或程序集引用?
我试图来分配电流
到从我的内容页面管理
李到我的母版页...任何建议...
I am trying to assign current
to Manage
li from my content page to my master page... Any suggestion...
推荐答案
像它说,有一个在 AttributeCollection $ C无
的containsKey
方法$ C>。
Like it says, there's no ContainsKey
method in AttributeCollection
.
更改code以下,它会做同样的事情:
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;
}
}
这篇关于在asp.net contentpage分配CSS类为母版控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文