我必须实现一种从ASP水平菜单(使用配置文件生成)中选择值的方法。菜单的生成不是问题:我的实际问题是为用户找到一种无需使用回发即可访问其值的方法。
我正在处理一个大型且古老的项目,其目的是完全不使用PostBack请求。 回发在设计上是意外的,被认为是错误。
为了达到我的目标,我尝试为MenuItemClick事件编写一个JavaScript触发器,以return false结尾,以避免回发(目前,JavaScript代码仅限于简单的测试警告和false return,用于测试目的)。但这不起作用:我无法获得触发JavaScript函数的事件:发生回发,这是不希望的。
完整的代码包含数千行(其中大多数与问题无关),这是仅包含相关行的精简版本:
Default.aspx:
<%@ Register TagPrefix="CRB" Namespace="ConfigurableReportBuilder.PageControls" Assembly="ConfigurableReportBuilder" %>
<form id="form1" class="page" runat="server">
<CRB:HorizontalMenu ID="MainMenu" runat="server" RootNode="Menu/Items" StyleClass="ui-menu ui-state-hover"/>
//this works fine, no issue here.
</form>
Default.aspx.cs:
(...)
if (this.IsPostBack)
throw new Exception("PostBack requests are not expected to occur");
//Ensure that "PostBack requests" (which are unexpected and therefore indicate bugs) become recognized by a specific exception which is thrown here
Workspace.js:
var WS = (function ($) {
var test = function (event) {
alert("test");
return false; //prevent postback
};
(...)
return {// This dictionary contains references to public methods used in the project
test: test,
(...)
}
}
$(document).on("menuitemclick", '#MainMenu', WS.test); //setting the event trigger
我已经找到了可以实现预期目标的解决方案,但是请注意,它不尊重编码的良好做法