Last post Jan 05, 2011 06:53 PM by Santa Ana Slim
Jan 05, 2011 12:46 PM|Santa Ana Slim|LINK
I've inherited a project and in effort to beautify it, I'm replacing an image based menu bar with a CSS based bar. When you click on a button it takes you to the appropriate page.
Currently in the OnInit() it checks what page you're on and then changes the URL of the image for the appropriate button to be the "active" image for that button:
btnFacilities.ImageUrl = "menu_front_facilities.gif";
Pretty simple, and works fine, but it's a pain to add new items, because I need to create a new image and if I want to change the look, I have to create a whole new slew of buttons.
I have built out the CSS menu using basic UL and LI and all the hovers and links work fine. My issue comes when I want to set the "active" item. The way the CSS works is by either setting the CLASS of the active item to "active" or I could easily change
it to set the ID of the item to "active", but in either case the viewstate crashes on any posts because the object is not the same as it was when the page loaded, since the style is updated when the OnInit(); event goes off.
I've tried both setting the class name:
as well as directly applying the CSS style:
menuFacilities.Style.Add("background", "#1376c9 url('images/topnav_active.gif') repeat-x;");
Both work fine on the initial page load, but once I click anything that posts, it crashes with a viewstate failure.
If I set the enableviewstate="false" then it works fine between different page loads, but not if I post to the same page (ie submit a search form)
I can't figure out how to handle the "active" object. For some reason the image URL works for asp:Imagebuttons
Here's what one of the items in the menu looks like (pretty simple stuff here):
<li id="menuFacilities" runat="server" ><a href="#">Facilities</a>
<span id="submenuFacilities" runat="server" >
<asp:LinkButton id="lnkFacilitySearch" runat="server" OnClick="lnkFacilitySearch_Click">Facility Search</asp:linkbutton>
<asp:LinkButton id="lnkThruputs" runat="server" OnClick="lnkThruputs_Click">Throughput Limits</asp:linkbutton>
Thanks in advance
Jan 05, 2011 06:53 PM|Santa Ana Slim|LINK
After spending many hours messing with things. I enabledviewstate="false" on my controls and that made everything work except when postbacks were made. I found a chunk of code that would check the current page and set the "active" item that was being called
only on when !postback, so set it to set the active items even on postback, probably not the most efficient, but it worked. I continued making changes move some stuff around and then the message started coming up again. I sort of lost track of what I had done
since I had made so many changes, so I couldn't figure out what exactly triggered the error again.
When all is said and done, It seems the issue was because the <DIV> that the <UL>s was in didn't have a runat=server in it. I didn't think it would matter, but I guess if you're going to have the <UL runat=server> then I suppose the parent container needs
the runat=server as well. Once I added that everything started working again. I even managed to remove all my enableviewstate=false just to clean things up and everything's currently working.... for now.