1

My master page has the menu. By clicking on the "home" link takes me to home.aspx page. I want the home button to be in a different css while I'm on that page. I came across multiple posts but this one from Jeremy caught my eye and looked simple enough.
https://stackoverflow.com/a/10871099/1851048 .

I tried to implement the same in my website but its not working. I get this: "'System.Web.UI.HtmlControls.HtmlGenericControl' is a type not a namespace"
This is the .cs code in my home.aspx.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");
      }
 }


I did some research here: http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlgenericcontrol.aspx
But I got different errors each time. I managed to get rid of all of them but then the code wouldn't work.
This is my first website so I currently have a lot to learn. Appreciate your help.

Edit:

 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>
Community
  • 1
  • 1
A_AR
  • 508
  • 2
  • 6
  • 20
  • check this may be help to you http://stackoverflow.com/questions/14023188/create-handler-that-manipulates-head-tag/14023310#14023310 – Sampath Dec 25 '12 at 15:07

1 Answers1

0

Here your problem is you don't need to create separate class for HtmlGenericControl and your namespace is also wrong (it should be HtmlControls).

Try with below code (replace your code with below).

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");

       }
   }

I hope this will help to you.

Sampath
  • 63,341
  • 64
  • 307
  • 441
  • I tried it and I got this error: Object reference not set to an instance of the object. This control I am trying to change the color of, is the asp:Hyperlink. – A_AR Dec 25 '12 at 15:51
  • can you put that whole error on your post ? and your final code also. – Sampath Dec 25 '12 at 15:54
  • To be more clear, I have done the following changes to the code as for my requirements: the control ID is "HyperLink1" and I have created a new class in my stylesheet "cssToApply" which should provide the style to my hyperlink control. – A_AR Dec 25 '12 at 15:56
  • "System.NullReferenceException: Object reference not set to an instance of an object." is the error and it saying that mycontrol.Attributes.Add("class", "cssToApply"); is the problem. – A_AR Dec 25 '12 at 15:58
  • can you put your final code on your original post not as a comment (very difficult to see when it's on comment section).Need to put your Html and also CSS. – Sampath Dec 25 '12 at 16:01
  • Done the editing. Notice the #menu a:hover style? I want the same style to be preserved when I navigate to the home page. – A_AR Dec 25 '12 at 16:09