0
votes

I am facing a problem while passing a string from JavaScript to ASPX WebMethod in C# through PageMethods.

The string in JavaScript is: var data = "781:7.00,2016:7.00,2727:4.00,2752:1.00,2941:5.00";

and i am calling as PageMethods.Myfunc(data)

How should i receive the string in C# Method , if i want the string as such ?

Thanks.

2
Can you post Myfunc C# function? - Arindam Nayak

2 Answers

0
votes
<script type = "text/javascript">
function GetData() {
    var Data= [781:7.00,2016:7.00]
    PageMethods.GetData(Data, OnSuccessGetData);
}
function OnSuccessGetData(response) {       
    for (var i in response) {
        alert(response[i]);
    }
}
</script> 

Call event on Click : 
     <asp:Button id = "button" onclick = "GetData()"   />

Code Behind :

[System.Web.Services.WebMethod]
public static string[] GetData(string[] Data)
{
return Data;
 }
-1
votes

IN Aspx Page

 <script type="text/javascript">
function invokeMethod() {
    x = document.getElementById('<%= TextBox1.ClientID %>').value;
    PageMethods.MyFunction(Data, OnSuccess, OnFailure);
}
function OnSuccess(r) {
    document.getElementById('<%= TextBox1.ClientID %>').value = r;
}
function OnFailure(r) {
    alert(r._message);
}

In C#

[System.Web.Services.WebMethod()]
 public static string MyFunction(string data )
 {
     return Data + "!!!";
 }