开发者

prevent firing the code-behind event from JavaScript?

开发者 https://www.devze.com 2023-03-22 07:40 出处:网络
I would like this ASP button control to stop calling its event handler when the JavaScript client method IsCorrectPrice() returns false.

I would like this ASP button control to stop calling its event handler when the JavaScript client method IsCorrectPrice() returns false.

<asp:Button ID="btnsubmit" runat="server" 
  开发者_开发百科          Text="Submit" OnClientClick="javascript:IsCorrectPrice()"/>


btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
  Handles btnsubmit.Click

How can this be done?


Change your OnClientClick to be like this:

OnClientClick="return IsCorrectPrice();"


If you have the option to use jQuery, you could use this..

$("#btnsubmit").click(function() {
   if (!IsCorrectPrice()) {
      e.preventDefault();
   }
});

*Note:

Using the OnClientClick right there in the DOM starts to get unmaintainable after the most basic scenarios.

Also, the #btnsubmit may not be the exact id of your button, but that depends on what version of ASP.Net you are using and if you are using 4, what your ClientIdMode is set to.

0

精彩评论

暂无评论...
验证码 换一张
取 消