ASP.NET 2.0 Menu – MenuItemClick not posting back/firing event

 

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.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值