【问题标题】:assigning css class to a masterpage control from contentpage in asp.net从 asp.net 中的 contentpage 将 css 类分配给母版页控件
【发布时间】:2011-03-18 20:59:29
【问题描述】:

我的母版页中有一个无序列表....

<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?

我正在尝试将 current 分配给 Manage li 从我的内容页面到我的母版页...任何建议...

【问题讨论】:

  • 关于母版页和内容页的非常好的问题。

标签: asp.net css master-pages html-lists


【解决方案1】:

就像它说的那样,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;
    }
}

【讨论】:

    【解决方案2】:

    我曾经遇到过类似的问题,但我认为这比你们俩在这里提出的要简单得多。要将 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。可以应用此技术,而无需使用导入或进行任何重大更改来继承母版页文件。

    我希望这会有所帮助,当我忘记如何执行此操作时,我现在有一个资源可以帮助我哈哈。

    【讨论】:

      猜你喜欢
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      相关资源
      最近更新 更多