Click here to Skip to main content
15,921,694 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have 2 textboxes ,one label control and one button on aspx page. I want to display result of two textboxes on the label control on buttons onclientclick event.

I have one web method and I am calling this method from javascript function.

Problem is that when I assign result(ie sum) in the label control then it visible only for 1 second even less than that and disappears. Even no server event is being fired after that.
can you please help.
My code is given below:
ASP.NET
  <script type="text/javascript">
        function CallSum() {
            var Num1 = $get('TextBox1');
            var Num2 = $get('TextBox2');
            var lblControl = $get('lblResult');

            //call server side function
            PageMethods.Sum(Num1.value, Num2.value, CallSuccess, CallFailed, lblControl);
        }
          function CallSuccess(result, lblControl) {
            //Show the result in lblresult
                lblControl.innerHTML = result;
           }

        function CallFailed(result, lblControl) {
            if (result !== null) {
                alert(result.get_message());
            }
        }
    </script>
First Number<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
   Second Number<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />

   Result = <asp:Label ID="lblResult" ClientIDMode="Static" runat="server"></asp:Label><br />
   <asp:Button ID="button1" runat="server" Text="Get SUM" OnClientClick="return CallSum();" />

and server method is:
C#
[System.Web.Services.WebMethod]
        public static int Sum(int n1, int n2)
        {
            try { return n1 + n2; }

            catch (Exception ex) { throw ex; }
        }

Thanks in advance.
Vivek.
Posted
Updated 24-Jan-13 3:23am
v4

you must return false in the callsum function.
 
Share this answer
 
Try:
ASP.NET
<asp:Button ID="button1" runat="server" Text="Get SUM" OnClientClick="CallSum(); return false;" />

This should just execute the client side code and stop the execution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900