简体   繁体   English

在提交按钮的OnClientClick事件上调用javascript函数

[英]calling javascript function on OnClientClick event of a Submit button

In my asp.net page, I have three text boxes and one button. 在我的asp.net页面中,我有三个文本框和一个按钮。
First two text boxes are being used to get the value from the user and third one is being used to show the sum of first two boxes. 前两个文本框用于从用户获取值,第三个文本框用于显示前两个框的总和。

I am calling a javascript function to sum the values and show the result value on OnClientClick event of that BUTTON. 我正在调用一个javascript函数来对值进行求和,并在该BUTTON的OnClientClick事件上显示结果值。

JS function is working fine but when I am pressing button to get the sum, it shows the result in third text box and just then the page is being post-back and result value becomes disappear from the third text box. JS函数工作正常但是当我按下按钮得到总和时,它会在第三个文本框中显示结果,然后页面正在回发,结果值将从第三个文本框中消失。

Why the post-back is called by the button? 为什么按钮会调用回发?
I don't want page to be post-backed on click of submit button. 我不希望页面在点击提交按钮后进行后备。

Any suggestion is appreciated. 任何建议表示赞赏。

OnClientClick="SomeMethod()" event of that BUTTON, it return by default " true " so after that function it do postback OnClientClick="SomeMethod()"事件的那个BUTTON,它默认返回“ true ”所以在那个函数之后它会回发

for solution use 用于解决方案

//use this code in BUTTON  ==>   OnClientClick="return SomeMethod();"

//and your function like this
<script type="text/javascript">
  function SomeMethod(){
    // put your code here 
    return false;
  }
</script>

The above solutions must work. 上述解决方案必须有效。 However you can try this one: 但是你可以尝试这个:

OnClientClick="return SomeMethod();return false;"

and remove return statement from the method. 并从方法中删除return语句。

<asp:Button ID="btnGet" runat="server" Text="Get" OnClick="btnGet_Click" OnClientClick="retun callMethod();" />
<script type="text/javascript">
    function callMethod() {
        //your logic should be here and make sure your logic code note returing function
        return false;
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Javascript触发ASP按钮上的“ onClick”和“ onClientClick”事件 - Trigger both 'onClick' and 'onClientClick' event on an ASP button from Javascript JavaScript OnClientClick破坏了RadGrid的“插入/更新”按钮在Web表单上提交行为 - JavaScript OnClientClick breaking RadGrid Insert/Update button Submit behavior on web form 从JavaScript中的OnClientClick事件中阻止C#中的asp:Button OnClick事件? - Block an asp:Button OnClick event in C# from the OnClientClick event in JavaScript? Asp .NET Button - OnClientClick =“return function()”vs OnClientClick =“function()” - Asp .NET Button - OnClientClick=“return function()” vs OnClientClick=“function()” 单击按钮时,不会引发OnClientClick事件 - When clicking a button, the OnClientClick event does not raise 如何在 onClientClick 事件的函数内评估值 - How to eval value inside a function on onClientClick event 按钮提交前的Javascript函数 - Javascript function before button submit 调用javascript函数&amp;&amp;处理事件 - calling a javascript function && handling an event 一起按钮的OnClientClick和Click事件没有触发 - 在FireFox中出现问题 - OnClientClick and Click event together for a button is not firing - issue in FireFox 在c#Button事件中一起调用OnClientClick和Onclick - call OnClientClick and Onclick together in c# Button Event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM