Click here to Skip to main content
11,410,750 members (63,849 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Hi I want that when i take the mouse over the menu, I dont want to show submenu.the submenu should only be display on clicking the parent item. I have writen code as
 <asp:Menu ID="Menu1" runat="server" BackColor="#FFFBD6" DynamicHorizontalOffset="2"
            Font-Names="Verdana" Font-Size="0.8em" ForeColor="#990000" Orientation="Horizontal" StaticSubMenuIndent="10px" Style="z-index: 112; left: 205px;
            position: absolute; top: 14px">
            <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <DynamicHoverStyle BackColor="#990000" ForeColor="White" />
            <DynamicMenuStyle BackColor="#FFFBD6" />
            <StaticSelectedStyle BackColor="#FFCC66" />
            <DynamicSelectedStyle BackColor="#FFCC66" />
            <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <StaticHoverStyle BackColor="#990000" ForeColor="White" />
        </asp:Menu>
    and in the code behinde file as   protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            DataTable dt = new DataTable();
            DataTable dt1 = new DataTable();
 
            // DataTable dt2 = new DataTable();
 
            TextBox3.Visible = false;
            Label3.Visible = false;
            Label2.Text = "Password";
            // Menu m = new Menu();
            string cn = "Data Source=hcl;Initial Catalog=mydata;Integrated Security=True";
            SqlConnection con = new SqlConnection(cn);
            SqlCommand cmd = new SqlCommand("select parentname,id from menu ", con);
            con.Open();
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            dt.Load(dr);
            for (int count = 0; count < dt.Rows.Count; count++)
            {
 
                MenuItem mnuLinkItem = new MenuItem();
                mnuLinkItem.Text = dt.Rows[count]["parentname"].ToString();
                mnuLinkItem.Value = dt.Rows[count]["id"].ToString();
                string menuid = mnuLinkItem.Value;
                // mnuLinkItem.NavigateUrl = datatable.Rows[count][1].ToString();
 
                Menu1.Items.Add(mnuLinkItem);
 
                string str = "select subname from submenu where subid=" + menuid + "order by menuorder asc";
                SqlCommand cmd1 = new SqlCommand(str, con);
                cmd1.CommandType = CommandType.Text;
                SqlDataReader dr1 = cmd1.ExecuteReader();
                dt1 = new DataTable();
                dt1.Load(dr1);
 
                int count2 = dt1.Rows.Count;
                for (int j = 0; j < count2; j++)
                {
 
                    MenuItem mnuLinkItem1 = new MenuItem();
                    mnuLinkItem1.Text = dt1.Rows[j]["subname"].ToString();
                    mnuLinkItem.ChildItems.Add(mnuLinkItem1);
                    // mnuLinkItem.NavigateUrl = datatable.Rows[count][1].ToString();
 
                    // mnuLinkItem1.ChildItems.AddAt(count2, mnuLinkItem1); //meun.Items.Add(mnuLinkItem);
                    //Menu1.Items.AddAt(count2, mnuLinkItem1);
                    //dr2.Close();
                }
 

            }
            con.Close();
 
        }
    }
Please help I want a exact code for it please help..
Posted 29-Sep-10 7:51am
Edited 29-Sep-10 8:32am
Abhinav S394.1K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here is a blog post that shows a solution where you override the menu javascript on your page:

This treat is for those that really hate how the ASP.NET Menu works. The menu shows the sub-menu when the user hovers the mouse over the root level menu items. This can get annoying especially if you accidentally pass the mouse over the menu when trying to click something inside the page. This can be really frustrating from a usability point of view especially when developing Web Applications (not just websites).
So, the following javascript will override the default behavior of the menu and show the second level of menu items only when the user clicks on the first (root) level menu items.

ASP.NET Menu, Click instead of Hover[^]




An alternative solution would be to create class that inherits from Menu and override the render replace the javascript emitted:

//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);
 
}
  Permalink  
v2
Comments
sahuanil at 1-Oct-10 12:21pm
   
thanks,the answer in the link ASP.NET Menu, Click instead of Hover[^],is working.but i didnt understand it.It is very complicated.but the code which i have understand is not working that is
html = html.Replace("onmouseover="Menu_HoverStatic(this)""","onclick="Menu_HoverStatic(this)""");
this line is showing error as ) expected when i move the cursor in onmouseover="Menu_HoverStatic(this)"""
and ; expected when i move the cursor in "onclick="Menu_HoverStatic(this)""");
please help .
William Jordan at 1-Oct-10 12:56pm
   
I fixed the offending line

it should have been:

html = html.Replace("onmouseover=\"Menu_HoverStatic(this)\"", "onclick=\"Menu_HoverStatic(this)\"");
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I have overridden the render method in a custom control, but sub menu is still not displaying when the parent menu is clicked. Pls help.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,920
1 OriginalGriff 6,918
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web01 | 2.8.150414.5 | Last Updated 1 Oct 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100