Last post Mar 26, 2010 08:36 AM by technodotnet
Jul 26, 2007 04:14 PM|larrydotnet|LINK
Is there a way to display submenues by just clicking the parent menu insted of the default behavior (mouse over it) in the Menu Control?
I need to prevent showing the menu structure when the user moves over the menu, it is very confusing to have submenues displayed the the user just need to move the mouse to reach another page option. Please is you know to to do that please let me know.
The real click event needs to be just on the the leaf menu (last one of each structure).
Please, give me some advise.
Jul 26, 2007 05:23 PM|Rodashar|LINK
What you could do is create a custom user control and add a Menu control to it. Then in the code override the render method and replace the onmouseover with the onclick. (Example below). The only troble with this would be if you were using the SiteMap to
fill your menu. If the SiteMapNodes have a NavigateUrl assigned then they may use the onclickevent. I'd have to look into this further. I'm not sure what you would do for closing the menu. I guess you could just leave the on mouseout and it would work. In
//Override the Render method to replace the onmouseover
Aug 22, 2007 05:37 PM|larrydotnet|LINK
Thank you very much. What i did was to create a class that inherits from Menu and overrided the Render metohd using your code. that worked for me.
Aug 22, 2007 05:47 PM|Rodashar|LINK
Glad I could be of help :D
Sep 03, 2008 10:16 AM|Mak1234|LINK
My requirement is also same. I need to display menu on Onclick event of mouse in my ASP.Net 2.0 application. But when I tried as you suggested it doesnt work for me. I have just render method into my masterpage wher actually menu is display but it is
not showing mwnu on ONCLICK event.
Sep 03, 2008 12:00 PM|Rodashar|LINK
You have to make sure to create a custom control and inherit the asp.NET menu control. You have to override the render method in your new custom control and not the master page. Can you include your code so I can take a look at what your doing?
Sep 04, 2008 11:33 AM|Mak1234|LINK
Thanks for your reply. Yes, I was trying to override in masterpage, I think that was my mistake. But I really dont have much experience about custom control. Can you please send some sample code for custom control which implement .Net menu class so
that I can modify it for my purpose. OR can you please send me any Url where I can find abt how to implement menu in custom control using .Net 2.0 and c#. If this is dome then I can override render method in cstom control.
Sep 04, 2008 12:02 PM|Rodashar|LINK
What you have to do is right click on your project file and select add new item. Select the class object and name it whatever you want and click Add. Visual Studio may tell you that it is a good idea to put classes in the App_Code folder. Just click yes
and go on your marry way. You should now have a class that looks like the snippet below
Public Class CustomWebMenu
Typically I would add some import statements for System.Web.UI.Webcontrols. Now you have to add the Inherits statement to let the class use the existing menu controls features. so your class would look like this
Public Class Class1
Once you have added this you can add the render code in the previous post reference your new control and vola! I hope that helps.
Sep 04, 2008 01:39 PM|Mak1234|LINK
Thanks for reply,
Upto this I was already had some code implemented but main thing is how to dynamically populate data and how to reference this class into a .aspx page ? Should I create new project for custom control OR just add new class into existing web
project ? If I have add only a class file as you suggested then how to give reference of this new class into a .aspx page ?
I am very much new in custom control so may be these are very basic question for you. But please answer me in detail.
Sep 04, 2008 04:12 PM|Rodashar|LINK
I would put the code in a single class file in your asp.NET project. In order to register the control with the page you want to use it on you need to add a Register tag to the top of you .aspx page. It would look like this.
<%@ Register NameSpace="ControlNamespace" TagPrefix="cc1" %>
I used a namespace in my control so I referenced it like this. Not sure how to do it without a namespace but adding a namespace is easy so it's a non issue. then to add your control to the page you would open a tag and enter cc1: it would look kinda like
<cc1:NameOfYourClass id="CustomControl1" ...>
Sep 05, 2008 11:51 AM|Mak1234|LINK
This is really helpful for me. Just one more thing can you send me the sample code to render the actual menu and submenu dynamically from XML file OR may be from database. and How I can render it in custom control ? I need
to render menu and submenu in custom control to test whether onClick is working or not.
Sep 05, 2008 12:05 PM|Rodashar|LINK
I personally use the sitemap and sitemap datasource objects. You must first add and setup a sitemap object. see here (http://msdn.microsoft.com/en-us/library/yy2ykkab.aspx) for more information on the sitemap object. Once you have the site map setup just
add a sitemap data source object to your .aspx page and set the menus DataSourceID property to the newly added sitemapdatasource.
Sep 15, 2008 01:20 PM|skkulla|LINK
I was trying to achieve the same thing by creating custom control and override the render but when i click my parent node menu on my menu control the page gets refreshed but submenu does not show up.
Can you please let me know why its happening.
Aug 27, 2009 02:19 AM|nuwan_dammika|LINK
Thanks alot. It heleped me in big way
Dec 07, 2009 04:11 PM|NickLocke|LINK
This looked like exactly what I wanted to do --- forcing the user to click rather than mouseover. I have implemented the code exactly as suggested, but saw no difference in the rendering.
With a bit of debugging, I got my hands on the HTML string in the Render method - and there lies my problem.
I am using .NET4 and there is no "onmouseover" code anywhere to be seen.
I'm guessing that it has been buried away in an ASD resource somewhere.
Mar 26, 2010 08:36 AM|technodotnet|LINK
I created a ASP.NET 3.5 Server Control class that inherits from Menu control and overrided the Render metohd using your code above.
But submenu does not show up on Clicking the parent node in the Menu. Can you tell me why? Here is the Menu server control code-
public class ServerControl1 : Menu
//Override the Render method to replace the onmouseover
protected override void Render(HtmlTextWriter writer)
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
string html = stringBuilder.ToString();
html = html.Replace("onmouseover=\"Menu_HoverStatic(this)\"", "onclick=\"Menu_HoverStatic(this)\"");
And here is the code for Default.aspx where I am showing the above menu control -
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register assembly="ServerControl1" namespace="ServerControl1" tagprefix="cc2" %>
<cc2:ServerControl1 ID="ServerControl11" runat="server" >
<asp:MenuItem Text="File" Value="File">
<asp:MenuItem Text="New" Value="New"></asp:MenuItem>
<asp:MenuItem Text="Open" Value="Open"></asp:MenuItem>
<asp:MenuItem Text="View" Value="View">
<asp:MenuItem Text="Normal" Value="Normal"></asp:MenuItem>
<asp:MenuItem Text="Preview" Value="Preview"></asp:MenuItem>