1

I am trying to change the css of the content page which inherits its css from the master page. I tried the below but for some reason I am getting the "Object reference not set to an instance of an object" error.

Below is my cs code:

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

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

Find the masterpage content here:

    <div id="menu">
        <ul id="ul1" runat="server">
            <li id="li1" runat="server">
                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl ="home.aspx">Home</asp:HyperLink></li>
            <li id="li2" runat="server">
                <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl ="AboutUs.aspx">About Us</asp:HyperLink></li>
            <li id="li3" runat="server">
                <asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/courses.aspx" >Courses</asp:HyperLink></li>
            <li id="li4" runat="server">
                <asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/News.aspx" >News</asp:HyperLink></li>
            <li id="li5" runat="server">
                <asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="ContactInfo.aspx" >Contact Us</asp:HyperLink></li>
        </ul>
    </div>

The css as below:

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

 .newCSS
{
    background-color: black;
    color: #fff;

}

I tried giving the .FindControl ID as the HyperLink ID instead of the ul, li Id as shown above but that did not work either.

Edit: Tried the following in the cs page:

protected void Page_Load(object sender, EventArgs e)
{
        HtmlGenericControl mycontrol = (HtmlGenericControl)Master.FindControl("ul1").FindControl("li1"); 
        mycontrol.Attributes.Add("class", "newCSS");    
}

No compiler error but the code does not function.

A_AR
  • 498
  • 2
  • 6
  • 20
  • Have you tried doing the following WebControl mycontrol = (WebControl)Page.FindControl("ul1"); – MethodMan Dec 28 '12 at 19:18
  • @DJ KRAZE Yes I tried it. I get the same error. – A_AR Dec 28 '12 at 19:22
  • have you tried moving the runat="server" up as Thousand has suggested – MethodMan Dec 28 '12 at 19:24
  • @DJ Yes I did that. Let me just edit it in the question. It was a simple mistake I made while playing around for a solution before posting the question. – A_AR Dec 28 '12 at 19:26
  • HtmlGenericControl myControl = (HtmlGenericControl)Master.FindControl("ul1"); should do the trick – MethodMan Dec 28 '12 at 19:27
  • Almost all cases of NullReferenceException are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 28 '12 at 19:27
  • @DJ Ok no errors this time but the css of the menu did not change. The code did not function. – A_AR Dec 28 '12 at 19:29
  • update your code with the now working solution at the top of your original question – MethodMan Dec 28 '12 at 19:31
  • @DJ KRAZE I have updated the question after the changes. – A_AR Dec 28 '12 at 19:43
  • how come you have this runat="server" on ui1 and not the rest.. be consistent – MethodMan Dec 28 '12 at 19:48
  • @John Wonderful post and I now know that the problem is with finding the ul and li ID values. I tried troubleshooting by adding the "if" clause as well for a possible null value but that did not yield either. Any help regarding to this question is most appreciated. – A_AR Dec 28 '12 at 19:48
  • @DJ KRAZE Since I was only concentrating on the home page, I did not bother changing the other ones. But now that I have done, there was no change in the outcome :( – A_AR Dec 28 '12 at 19:52
  • No need to nest your `FindControl()` calls. Simply look for the `
  • ` item: `HtmlGenericControl mycontrol = (HtmlGenericControl)this.Page.Master.FindControl("li1") as HtmlGenericControl;`
  • – andleer Dec 28 '12 at 19:56
  • @andleer I tried that before posting the question here, andleer. I do not get an error but the code does not work. I am puzzled and clueless. – A_AR Dec 28 '12 at 20:02