ASP.NET 2.0 Menu – MenuItemClick not posting back/firing event
2007-07-30 13:45
645 查看
This can occur if you have set the MenuItem's Text property to HTML. The behavior occurs due to the way that MenuItem's end up being rendered into HTML. Each menu item has an <a href=""> link, so when you click on a MenuItem it's the same as clicking on any other link, except the href contains a "JavaScript:__doPostback" call, which will trigger the event on the server side.
Now, what happens is that when you set your Text property to a chunk of HTML, (a table, for instance), that link is being hidden by those elements you have set – causing any click on the menu item to be sent directly to that container, rather than invoking the link click.
A way around this, is to set the onclick of the outer container of your HTML for the MenuItem to something which will invoke the contents of the parents href script:
tbl.Attributes.Add("onclick", "eval(this.parentNode.parentNode.getAttribute('href').substring(11));");
The reason we are using parentNode.parentNode is because the HTML contents you set in your Text property is embedded inside a div. So we need to get to it's parent, which is the link containing the href with the postback script.
We then evaluate that javascript code we find in the href attribute, all except the "JavaScript:" qualifier. And voila! Our menu item's click event is raised…
Hope this helps someone else – it can get quite confusing if you don't understand how these things work under the hood.
相关文章推荐
- ASP.NET 2.0 Menu – MenuItemClick not posting back/firing event
- not firing itemclick event
- not firing itemclick event
- 关于ASP.NET里面的onmenuitemclick事件不被触发的问题
- asp.net OnClientClick not rendered for initially disabled Button
- ASP.NET 2.0构建动态导航的Web应用程序(TreeView和Menu ) (海东的技术资料)
- 父菜单高亮; ASP.NET Menu parent menuitem highlighting
- 扩展--&gt;为ASP.NET 2.0菜单控件(menu, repeater)增加target属性
- 转: Simple ASP.NET 2.0 Tips and Tricks that You May (or may not) have Heard About (一些简单的、你可能已经知道或者不知道的ASP.NET 2.0技巧)
- ASP.NET 2.0构建动态导航的Web应用程序(TreeView和Menu )
- 在Asp.net 2.0中关于menu和treeview的几点使用注意!
- ASP.NET 2.0构建动态导航的Web应用程序(TreeView和Menu )
- ASP.NET 2.0构建动态导航的Web应用程序(TreeView和Menu )
- ASP.NET 2.0构建动态导航的Web应用程序(TreeView和Menu )
- Unable to use SQL Server because ASP.NET version 2.0 Session State is not installed on the SQL serve
- ASP.NET 2.0提供了一个Menu 类,可以方便地创建水平或者垂直方向的下拉菜单,
- ASP.NET 2.0构建动态导航的Web应用程序(TreeView和Menu )
- ASP.NET 2.0构建动态导航的Web应用程序(TreeView和Menu )
- Simple ASP.NET 2.0 Tips and Tricks that You May (or may not) have Heard About [from dwahlin]
- [ASP.NET]LinkButton控件引起的“WebForm_PostBackOptions is not defined"