将 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”不包含“ContainsKey”的定义,并且找不到接受“System.Web.UI.AttributeCollection”类型的第一个参数的扩展方法“ContainsKey”(您是否缺少 using 指令或程序集引用?< /code>
我正在尝试将 current
分配给 Manage
li 从我的内容页面到我的母版页...任何建议...
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;
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如它所说,
AttributeCollection
中没有ContainsKey
方法。将您的代码更改为以下内容,它将执行相同的操作:
Like it says, there's no
ContainsKey
method inAttributeCollection
.Change your code to the following and it'll do the same thing:
我曾经遇到过类似的问题,但我认为它比你们在这里提出的问题要简单得多。要将 css 对象应用到从控件到母版页的服务器控件,您可以将其放入
控件页面中的
每个页面中,并在控件文件的代码后面
添加它不存储当前页面添加的类,它会被销毁一旦您离开页面并访问另一个页面,因此您无需担心创建重复
在这种情况下,“mycontrol”适用于列表项的导航菜单,我希望当前页面导航项在页面上突出显示。这不仅适用于 CSS class,还适用于 CSS id。无需使用导入或进行任何重大更改即可应用此技术来继承母版页文件。
我希望这会有所帮助,当我忘记如何做到这一点时,我现在有一个资源可以帮助我,哈哈。
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
in the code behind files of your controls
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
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.