如何在单击父菜单而不是鼠标悬停时显示菜单的子项 [英] how to show subitem of the menu on only clicking the parent menu not on mouse over

查看:72
本文介绍了如何在单击父菜单而不是鼠标悬停时显示菜单的子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我希望当我将鼠标移到菜单上时,我不想显示子菜单。只有在单击父项时才会显示子菜单。我写了代码为

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..

推荐答案

这是一篇博文,显示了一个覆盖菜单javascript的解决方案在你的页面上:



这个款待适合那些真正讨厌ASP.NET菜单工作方式的人。当用户将鼠标悬停在根级菜单项上时,菜单显示子菜单。如果您在尝试单击页面内的某些内容时意外地将鼠标移到菜单上,这可能会很烦人。从可用性的角度来看,这可能会非常令人沮丧,尤其是在开发Web应用程序(而不仅仅是网站)时。

因此,以下javascript将覆盖菜单的默认行为并显示第二级菜单仅当用户点击第一(根)级别菜单项时的项目。



ASP.NET菜单,点击而不是悬停 [ ^ ]








另一种解决方案是创建继承自Menu的类并覆盖渲染替换发出的javascript:



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

}


I have overridden the render method in a custom control, but sub menu is still not displaying when the parent menu is clicked.请帮忙。
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.


这篇关于如何在单击父菜单而不是鼠标悬停时显示菜单的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆