Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
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:
  <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:
[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 24-Jan-13 1:07am
Edited 24-Jan-13 4:23am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you must return false in the callsum function.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try:
<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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 504
1 Maciej Los 349
2 Kornfeld Eliyahu Peter 325
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100