Menu Control - Use OnClick instead of OnMouseOver

Last post 08-27-2009 2:19 AM by nuwan_dammika. 13 replies.

Sort Posts:

  • Menu Control - Use OnClick instead of OnMouseOver

    07-26-2007, 4:14 PM
    • Member
      273 point Member
    • larrydotnet
    • Member since 12-09-2002, 1:43 PM
    • Ecuador
    • Posts 108

    Hi everyone,

    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.

    Thanks.

     

    Larry Suárez.
  • Re: Menu Control - Use OnClick instead of OnMouseOver

    07-26-2007, 5:23 PM
    Answer
    • Member
      574 point Member
    • Rodashar
    • Member since 07-25-2007, 9:46 PM
    • Calgary
    • Posts 129

    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 theory you'd just call the same javascript function and it should work. Let me know if this helps.

     

    //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);

    base.Render(htmlWriter);

    string html = stringBuilder.ToString();

    html = html.Replace("onmouseover="Menu_HoverStatic(this)""","onclick="Menu_HoverStatic(this)""");

    writer.Write(html);

    }

    Remeber to mark as Answer if someone's answer helped you.
  • Re: Menu Control - Use OnClick instead of OnMouseOver

    08-22-2007, 5:37 PM
    • Member
      273 point Member
    • larrydotnet
    • Member since 12-09-2002, 1:43 PM
    • Ecuador
    • Posts 108

    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.

    Larry Suárez.
  • Re: Menu Control - Use OnClick instead of OnMouseOver

    08-22-2007, 5:47 PM
    • Member
      574 point Member
    • Rodashar
    • Member since 07-25-2007, 9:46 PM
    • Calgary
    • Posts 129

    Glad I could be of help :D

    Remeber to mark as Answer if someone's answer helped you.
  • Re: Menu Control - Use OnClick instead of OnMouseOver

    09-03-2008, 10:16 AM
    • Member
      8 point Member
    • Mak1234
    • Member since 09-03-2008, 2:08 PM
    • Posts 4

    Hi,

       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.

     

    MAK.

  • Re: Menu Control - Use OnClick instead of OnMouseOver

    09-03-2008, 12:00 PM
    • Member
      574 point Member
    • Rodashar
    • Member since 07-25-2007, 9:46 PM
    • Calgary
    • Posts 129

     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?

    Remeber to mark as Answer if someone's answer helped you.
  • Re: Menu Control - Use OnClick instead of OnMouseOver

    09-04-2008, 11:33 AM
    • Member
      8 point Member
    • Mak1234
    • Member since 09-03-2008, 2:08 PM
    • Posts 4

    Hi,

          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.

     

    Thanks,

    MAK

     

     

  • Re: Menu Control - Use OnClick instead of OnMouseOver

    09-04-2008, 12:02 PM
    • Member
      574 point Member
    • Rodashar
    • Member since 07-25-2007, 9:46 PM
    • Calgary
    • Posts 129

    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
       
    End Class

     

    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

     

    Imports System.Web.UI.WebControls

    Public Class Class1
        Inherits Menu


    End Class

     

    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.

    Remeber to mark as Answer if someone's answer helped you.
  • Re: Menu Control - Use OnClick instead of OnMouseOver

    09-04-2008, 1:39 PM
    • Member
      8 point Member
    • Mak1234
    • Member since 09-03-2008, 2:08 PM
    • Posts 4

    Hi,

         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.

    Thanks,

    MAK

                

  • Re: Menu Control - Use OnClick instead of OnMouseOver

    09-04-2008, 4:12 PM
    • Member
      574 point Member
    • Rodashar
    • Member since 07-25-2007, 9:46 PM
    • Calgary
    • Posts 129

     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 this

    <cc1:NameOfYourClass id="CustomControl1" ...>

    Remeber to mark as Answer if someone's answer helped you.
  • Re: Menu Control - Use OnClick instead of OnMouseOver

    09-05-2008, 11:51 AM
    • Member
      8 point Member
    • Mak1234
    • Member since 09-03-2008, 2:08 PM
    • Posts 4

    Hi Rodashar,

                    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.

     Thanks,

    MAK.

  • Re: Menu Control - Use OnClick instead of OnMouseOver

    09-05-2008, 12:05 PM
    • Member
      574 point Member
    • Rodashar
    • Member since 07-25-2007, 9:46 PM
    • Calgary
    • Posts 129

     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.

    Remeber to mark as Answer if someone's answer helped you.
  • Re: Menu Control - Use OnClick instead of OnMouseOver

    09-15-2008, 1:20 PM
    • Member
      2 point Member
    • skkulla
    • Member since 09-15-2008, 4:16 PM
    • Posts 1

    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.

     

    Thanks

  • Re: Menu Control - Use OnClick instead of OnMouseOver

    08-27-2009, 2:19 AM
    • Member
      4 point Member
    • nuwan_dammika
    • Member since 07-20-2008, 4:37 AM
    • Posts 2

    Thanks alot.  It heleped me in big way

Page 1 of 1 (14 items)