即使在javascript函数返回true之后,Codebehind方法也不会触发

时间:2015-06-21 07:43:59

标签: javascript c# asp.net

我的网络表单中有一个javascript函数,如下所示:

<asp:Button ID="btnRemoveNodeAssets" runat="server"  OnClientClick="return deleteNodeAssets();"
                                Text="Remove Item" OnClick="btnRemoveNodeAssets_Click" />

点击“删除”按钮

时会触发
deleteNodeAssets()

我面临的问题是,即使true返回btnRemoveNodeAssets_Click,我的代码隐藏方法class SMS { public string GetPort() { if (ErrorCondition) { throw new PortNotFoundException("Error finding port."); } return port; } public void ExecuteCommand(Command command) { if(ErrorCondition1) { throw new UnknownErrorException("Error Executing Command"); } else(ErrorCondition2) { throw new NoResponseException("No Response from device"); } } public void SendMessage(int number, string text) { FindPort(); ExecuteCommand(command); } } 根本不会触发。

1 个答案:

答案 0 :(得分:0)

Asp.net按钮不是提交类型按钮。

生成后按钮将为<input type="button" />

如果您的方法在OnClientClick中返回值,则onClick将不会触发。

因此,如果您的方法返回false,则返回值

如果reutrn为false则不执行任何操作,然后onClick将被调用

试试这个

OnClientClick="if(!deleteNodeAssets()) return false;"