您的位置:首页 > 编程语言 > ASP

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.

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐