1

我的母版页有菜单。通过单击“主页”链接将我带到 home.aspx 页面。当我在该页面上时,我希望主页按钮位于不同的 CSS 中。我遇到了多个帖子,但是 Jeremy 的这个帖子引起了我的注意,看起来很简单。
https://stackoverflow.com/a/10871099/1851048

我试图在我的网站上实现相同的功能,但它不起作用。我明白了:“'System.Web.UI.HtmlControls.HtmlGenericControl' 是一种类型而不是命名空间”
这是我 home.aspx.cs 中的 .cs 代码:

 using System.Web.UI.HtmlControls.HtmlGenericControl;
 using System.Web.UI.WebControls;


  public partial class _Default : System.Web.UI.Page

 {
protected void Page_Load(object sender, EventArgs e)
   {

   }

  }

 public class HtmlGenericControl : HtmlContainerControl
 {
    protected void Page_Load(object sender, EventArgs e)
      {
       HtmlGenericControls mycontrol = (HtmlGenericControl)this.Page.Master.FindControl("yourcontrolname") as HtmlGenericControl;

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


我在这里做了一些研究:http: //msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlgenericcontrol.aspx
但我每次都遇到不同的错误。我设法摆脱了所有这些,但是代码不起作用。
这是我的第一个网站,所以我目前有很多东西要学习。感谢你的帮助。

编辑:

 public partial class _Default : System.Web.UI.Page

 {
     protected void Page_Load(object sender, EventArgs e)
       {
           var mycontrol = this.Page.Master.FindControl("HyperLink1") as HtmlGenericControl;

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

       }

 }

CSS:

  #menu ul
 {
padding-bottom: 10px;
    padding-top: 10px;
list-style: none;
display: inline-block;
  }

   #menu li
   {    
margin: auto;
display:inline;
   }

 #menu a 
  {
    font-family: Verdana;
    color: white;
margin: 6px;
float: left;
width: 150px;
height: 27px;
padding: 0 0 0 0;
    border-radius: 6px;
text-align: center;
text-decoration: none;
font-size: 1.5em;
   }

 #menu a:hover
 {
   background-color: white;
   color: #bee2f1;
 }

 .cssToApply
 {
   background-color: white;
   color: #bee2f1;
  }

MasterPage.master:

                     <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="home.aspx">Home</asp:HyperLink></li>
4

1 回答 1

0

在这里,您的问题是您不需要创建单独的类,HtmlGenericControl并且您的命名空间也是错误的(应该是HtmlControls)。

试试下面的代码(用下面的代码替换你的代码)。

using System;    
using System.Web.UI.HtmlControls;


  public partial class _Default : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {
          var mycontrol = this.Page.Master.FindControl("yourcontrolname") as HtmlGenericControl;

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

       }
   }

我希望这对你有帮助。

于 2012-12-25T15:28:03.487 回答