How to get transparent backgrounds in an ASP.NET Menu Control's DynamicMenuItem
I'm working on this project currently, and the client wants translucent backgrounds on the drop-down menu.
I'm creating this website in ASP.NET 2.0, using Visual Studio.NET 2005. The menu is dynamically created and added to the page in an empty panel control.
The Problem:
I'm using a skin file and css to control the styles, and although everything is set up correctly, the background of the Dynamic Menu Items is still not translucent when rendered in IE. In Firefox, it renders correctly.How have I set this up?
In my skin file, notice that the DynamicMenuStyle (the div that contains all of the divs that make up the individual DynamicMenuItems) the BackColor is set to Transparent:
<asp:Menu runat="server" orientation="Horizontal" CssClass="menu" >
<StaticSelectedStyle CssClass="menuItemSelected" />
<StaticMenuItemStyle CssClass="menuItem" />
<DynamicHoverStyle CssClass="dynItemHover" />
<DynamicMenuStyle BackColor="Transparent" />
<DynamicMenuItemStyle CssClass="dynItem" />
</asp:Menu>
In the css, take note that the background-image is set to a semi-transparent png. Since the background of the div containing the Dynamic Menu Items is transparent, the translucent png should show the page behind it:
.dynItem
{
background-image:url(../../images/transpix.png);
border-bottom: solid 1px #EEE;
color: White;
height: 22px;
padding: 1px 15px 0px 5px;
display: block;
width: 100%;
}
The reason this doesn't work is that the rendered html code only ever sets the div that contains the dynamic menu's background-color to white, no matter what you do. I've read that you can override the pre-render function of the control, but honestly, the Menu control ought to just work.
Here's a snippet from the rendered HTML code:
.ctl00_ctl03_0 { background-color:white;visibility:hidden;display:none;
position:absolute;left:0px;top:0px; }
That represents the style of container div that holds the dynamic menu (the bit that pops up when you mouse over the static part).
The Solution:
I initially spent hours searching online only to find nothing of immediate use. All other solutions would added many more hours on to the project.
Eventually, I accidentally found the solution, and it's a simple one. Change the DynamicMenuStyle's Width to zero. The Dynamic Menu Items contained within the div will still display, despite being within a 0-width container. However, the annoying white background disappears.
So, in my skin file, I made this change...
<DynamicMenuStyle BackColor="Transparent" Width="0" />
... et voila, it fucking works.







18 Comments:
That was perfect!!!!!
It was so clever to do so ;)
Thanks a lot, you saved the day :)
No problem!
I was so relieved when I finally figured it out. It's not a real big issue, but I had a client who insisted on having the transparent background.
Glad I could help :)
OMG finally!!!
Many many many many many thanks!!!
Made my christmas merry!!!
/Mattias
No problem... I still remember the day it finally hit me... it was actually accidental. I was complaining to a co-worker about how there was no way to accomplish it, and as I was doing so just frustratedly making changes... suddenly it was working.
I had to go through my steps backwards to figure out what made it work.
Beijing,
Where are you setting the menu's backgroup color. In your example screenshots it appears to be green when not selected and red when selected.
Thanks,
Steve
Me again. To clarify my previous post. Are you using a DynamicItemTemplate to render the menu text over top of the semi-opaque png? Mind sharing that snippet?
Thanks again?
Dear Beijing Kenn, nice work!!!! But I have a problem. In my case for not know to me reason the background image gets duplicated.
Any ideas????
Thanks for your article Once I implemented this This are working nice but only A space appear when I add DynamicMenuStyle-Width="0" or any value
Can you give some idea to solve this problem
Thanks for your article Once I implemented this This are working nice but only A space appear when I add DynamicMenuStyle-Width="0" or any value
Can you give some idea to solve this problem
Great job on your blog post.
You just saved me lots of time and frustation on this.
Thanks
Yes!! Thanks so much for this solution. It worked for me and saved me a lot of time and frustration!
Hi Dude,
In my case for not know to me reason the background image gets duplicated.
Plz Help.
Oh, finally, you're a damned genius! Thanks man!
I love you man!! it was soo frustrating!! thank god (and you) that it's over
It does not working again in IE8 :(
When we tried this the behaviour in IE is that the menu does what you tell it to - its width goes to zero and none of the submenu items are displayed.
Hey everyone...
I don't know if you're still paying attention to my blog, as I haven't updated in so long.
I'm in China, and blocked from blogger. I can get through using a proxy, but it's not as fun and easy as it used to be.
However, I want to address some issues, especially since I got a lot of comments on this one... I am looking into your feedback!
I notice a lot of you are saying that it's not working in IE. I'm looking at a site that I've made:
http://www.jtnfa.com
It appears to work in IE 7 and 8 for me.
When you say it's not working, what results are you getting?
Post a Comment
Links to this post:
Create a Link
<< Home