Question : a code behind method that can be called from a javascript and return list of strings

I have an aspx page that has a javascript and I wanted to use a code behind method that can accept parametrs and returns list of strings based on some operation to be performed. Here I am more interested to ensure the wiring between the javacript and the code behind method. Moreover I want to use the value returned from the web method in the java script for additional operations . Hence I have to store the value somewhere like in a hidden variable or some other variable. I have tried a webmethod but I am facing the problem of not getting the value returned from the web method outside of the onsuccess call back function . You can see the following code.

SO my question is do I have any other better option. or it that impossible to use the value retured by a web method ouside of the onsuccess call back fucntion? I need a help please




1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
function webMethodCaller( ids){
         PageMethods.FormatName(ids,  onFormatNameSuccess,  onFormatNameFail,     onFormatNameTimeout);        
       
// i want to do something else based on result after this
//if I try to get the value in a hidden filed in here it is empty
}

 function onFormatNameSuccess(response)
{
      
                  
      var res="";
      
      for(var i=0,il=response.length; i<il;i++){
      res+=response[i];
      }
      
// i am trying to store the value in a hidden field for later use
      document.getElementById("HiddenField1").value = res;
//if i try to see the vaue of the hidden fiedl in here it is there but not ouside this fucntion
}

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;
        }

Answer : a code behind method that can be called from a javascript and return list of strings

>but how can i extract it from the xml ?
Use : xmlhttp.responseXML.getElementsByTagName("string")[0].childNodes[0].nodeValue
1:
2:
3:
4:
5:
6:
7:
function webMethodCaller( ids){
     if(window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     xmlhttp.open("POST","Dedicated.asmx/FormatName",false);
     xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
     xmlhttp.send(encodeURI("featureIds=" + ids));
     document.getElementById("<%= HiddenField1.ClientID %>").innerHTML = xmlhttp.responseXML.getElementsByTagName("string")[0].childNodes[0].nodeValue;
}
Random Solutions  
 
programming4us programming4us