Solved

webmethod return value in asp.net

Posted on 2010-08-18
14
1,754 Views
Last Modified: 2012-06-21

 In the following code my aim is to get a value from a web method and use it for further operations inside a javascript. However I cannot do so because when I cannot somehow store the returned value in a hidden filed.


function webMethodCaller( ids){
         PageMethods.FormatName(ids, 
      onFormatNameSuccess, 
      onFormatNameFail, 
      onFormatNameTimeout); 
      
            
        result=document.getElementById("HiddenField1").value;
 
// i want to do something else based on result after this
}



 function onFormatNameSuccess(response)
{
      debugger;
                  
      var res="";
      
      for(var i=0,il=response.length; i<il;i++){
      res+=response[i];
      }
      
      document.getElementById("HiddenField1").value = res;

}

function onFormatNameFail (exception, ctx, methodName) 
{ 
      alert("Method:"+ methodName +"\n"+ 
            exception.get_exceptionType() +"\n"+
            exception._message); 
}
function onFormatNameTimeout () 
{ 
      alert("Timeout"); 
}








///////////////////////////////////////////////

   <form id="form1" runat="server">
        <asp:HiddenField ID="hdnReturnValue" runat="server" Value="xx" />
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <input type="hidden" id="hiddnvalue" runat="server" value="" />
        <asp:ScriptManager ID="yourScriptManager" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
    </form>


the web method is the folloiwng
///////////////////////////////////////

 [WebMethod, ScriptMethod]
        static public List<String> FormatName(string featureIds)
        {
           

            string[] rawdata = featureIds.Split(new char[] { ',' });

            List<String> strings = new List<String>();
            for (int i = 0; i < rawdata.Length; i++)
            {
                strings.Add(rawdata[i]);
            }
            // strings.Add(FirstName);
            // strings.Add(LastName);*/

            //Session["result"] = strings;
            //   hdnReturnValue.value = strings;
          //  HiddenField1.Value = featureIds;
           // hiddnvalue.Value = featureIds;
            return strings;
            // LastName + ", " + FirstName;

        }

Open in new window

0
Comment
Question by:AbebeDemeke
  • 9
  • 5
14 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 33468177
Why don't wait the call of onFormatNameSuccess and do what you want ?
0
 

Author Comment

by:AbebeDemeke
ID: 33468315
leakim971: the thing is , is there any other way to get the value returned from the webmethod than storing somewhere inside the onFormatNameSuccess ?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33468344
What about :


function webMethodCaller( ids){
  PageMethods.FormatName(ids, onFormatNameSuccess, onFormatNameFail, nFormatNameTimeout);
}

function part2(result) {
//result=document.getElementById("HiddenField1").value;

// i want to do something else based on result after this

}

function onFormatNameSuccess(response) {
  result = document.getElementById("HiddenField1").value;
  var res="";
  for(var i=0,il=response.length; i<il;i++){
    res+=response[i];
  }
  part2(res);
}

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:AbebeDemeke
ID: 33468452
leakim971: the issue is I want the value stored somewhere. Again I tried to store in your part2 method as :
function part2(result) {
  document.getElementById("HiddenField1").value=result;

// i want to do something else based on result after this

}

function onFormatNameSuccess(response) {
  result = document.getElementById("HiddenField1").value;
  var res="";
  for(var i=0,il=response.length; i<il;i++){
    res+=response[i];
  }
      part2(res);

}



but still the value is not stored in the hidden variable
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33468532
set two alert and give me news :

function part2(result) {
alert("ok1");
  document.getElementById("HiddenField1").value=result;
// i want to do something else based on result after this
alert("ok2");
}
0
 

Author Comment

by:AbebeDemeke
ID: 33468590
leakim971: both ok1 and ok2 are displayed
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33468830
And what about :

function part2(result) {
alert(result);
  document.getElementById("HiddenField1").value=result;
// i want to do something else based on result after this
alert(document.getElementById("HiddenField1").value);
}
0
 

Author Comment

by:AbebeDemeke
ID: 33469578
there seems to be a mess in here . the alert displays the value in this function but when i try to do it outside of this function . lets say inside the function that calls this the webmethod then it displays empty string
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33471285
strange... I get "test" in alert with the following :

 
<script language="javascript" type="text/javascript">

    function pageLoad(sender, arg) {
        webMethodCaller("test");
    }

    function webMethodCaller(ids) {
        PageMethods.FormatName(ids, onFormatNameSuccess, onFormatNameFail, onFormatNameTimeout);
    }

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

    function onFormatNameSuccess(response) {
        result = document.getElementById("<%= HiddenField1.ClientID %>").value;
        var res = "";
        for (var i = 0, il = response.length; i < il; i++) {
            res += response[i];
        }
        part2(res);
        alert(document.getElementById("<%= HiddenField1.ClientID %>").value);
    }

    function onFormatNameFail(exception, ctx, methodName) {
        alert("Method:" + methodName + "\n" + exception.get_exceptionType() + "\n" + exception._message);
    }

    function onFormatNameTimeout() {
        alert("Timeout");
    }

</script>

Open in new window

0
 

Author Comment

by:AbebeDemeke
ID: 33474327
leakim971: yes that is my question . if u do alert inside  onFormatNameSuccess after calling part2 or inside part2 itself things seems fine but if you do alert inside the webMethodCaller like :

 function webMethodCaller(ids) {
        PageMethods.FormatName(ids, onFormatNameSuccess, onFormatNameFail, onFormatNameTimeout);
 alert(document.getElementById("<%= HiddenField1.ClientID %>").value);
    }

then you wont get anything. that is my question
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33474356
When using this :

 function webMethodCaller(ids) {
        PageMethods.FormatName(ids, onFormatNameSuccess, onFormatNameFail, onFormatNameTimeout);
        alert(document.getElementById("<%= HiddenField1.ClientID %>").value);
}

The alert is called immediately after PageMethods.FormatName. The ajax call is not ended and the value (HiddenField1) you try to read not updated.
That's why you must wait for the end of the ajax call before trying to do something relative with its final job (which is update the HiddenField1)

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33475295
check this : http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/JSON/Q_26348289.html?cid=748#a33265044

If you want to do a synchrone call to be able to get value of the HiddenField just after the ajax call (PageMethod) IN THE SAME FUNCTION, forget PageMethod and use :
http://kpumuk.info/asp-net/synchronous-page-method-call-in-asp-net-ajax-library/
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33475651
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33488918
Thanks for the points!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linq asp.net mvc 13 17
Add or delete table rows 10 53
ASP.NET (VB) return a record 2 37
Please explain purpose of GZIP 4 34
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question