5

在我的 asp.net 页面中,我有三个文本框和一个按钮。
前两个文本框用于从用户那里获取值,第三个文本框用于显示前两个框的总和。

我正在调用一个javascript函数来对这些值求和并在OnClientClick该按钮的事件中显示结果值。

JS 函数工作正常,但是当我按下按钮获取总和时,它会在第三个文本框中显示结果,然后页面正在回发,结果值从第三个文本框中消失。

为什么按钮调用回发?
我不希望页面在单击提交按钮时被后置。

任何建议表示赞赏。

4

3 回答 3

21

OnClientClick="SomeMethod()"该按钮的事件,它默认返回“ true”,所以在该函数之后它会回发

用于解决方案

//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>
于 2013-06-07T08:15:34.700 回答
4

上述解决方案必须有效。但是你可以试试这个:

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

并从方法中删除 return 语句。

于 2013-06-07T08:29:01.210 回答
2
<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>
于 2013-06-07T08:28:20.917 回答