Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Since I'm new to web development, I'm learning few things by trying out myself.

Anyway, I've a web service which returns the sum of two numbers & I've called this web service in my client code. The web service runs fine so does everything else. However, the problem is when I set the result retrieved from the Web service to the text box; at run time, the text box just flashes for a second displaying the result & then it automatically clears itself off. Meaning, I can the result but it vanishes quickly. Has it got anything to with Post Back or something similar on those lines?

I've pasted the code below.

XML
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="WS.From.Client.Script.aspx.cs" Inherits="ASP.NET.UI._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="~/MyArithmeticService.asmx" />
        </Services>
    </asp:ScriptManager>
    <asp:Button ID="btnCallWS" runat="server" Text="Call WS" OnClientClick="GetNumbersToAdd()" />
    <script type="text/javascript">

        function GetNumbersToAdd() {
            var firstNum = document.getElementById("<%= txtFirstNum.ClientID %>").value;
            var secondNum = document.getElementById("<%= txtSecondNum.ClientID %>").value;

            ASP.NET.UI.MyArithmeticService.AddTwoNumbers(firstNum, secondNum, DisplayAdditionResult);
        }

        function DisplayAdditionResult(result) {
            document.getElementById("<%= txtResult.ClientID %>").value = result;
        }

    </script>
    <asp:TextBox ID="txtFirstNum" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtSecondNum" runat="server"></asp:TextBox>
    <asp:Label ID="lblResult" runat="server" Text="Addition Result"></asp:Label>
    <asp:TextBox ID="txtResult" runat="server"></asp:TextBox>
</asp:Content>

</code>


Thanks in advance!
Ads
Posted
Comments
ZurdoDev 19-Feb-13 10:13am    
Likely due to an asp button which wants to do a postback. You can change your asp:button to a input type="button" instead or in your OnClienClick you can try doing "GetNumbersToAdd();return false;" to prevent the button from postingback.
Akash_ds 19-Feb-13 11:05am    
Thanks a ton, Ryanb31! You rock! Your solution worked absolutely fine! By the way, in the line of code: "GetNumbersToAdd();return false;" what does "return false" indicate? Does it instruct the browser to not to do a post back? If so, can "return false" be used in those places where in we'd like to prevent post backs? Please clarify. Thanks in advance!

1 solution

You're using a server-control button to call your GetNumbersToAdd() javascript function. Once it executes the client code it's then performing a postback. If you have not need of a postback I'd just just a regular html button or an input element with type of button.
 
Share this answer
 
v3
Comments
Akash_ds 19-Feb-13 11:07am    
Thanks Rahkan! I tried using a regular html control, it worked absolutely fine. Thank you so much!

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