Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
XML
<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
Updated 29-Sep-10 7:32am
v2

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

}
 
Share this answer
 
v2
Comments
software_Engi08 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 .
[no name] 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)\"");
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900