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.