Click here to Skip to main content
13,141,637 members (49,432 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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) {
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:
        public static int Sum(int n1, int n2)
            try { return n1 + n2; }
            catch (Exception ex) { throw ex; }

Thanks in advance.
Posted 24-Jan-13 0:07am
Updated 24-Jan-13 3:23am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

Solution 2

<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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2017
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