2

PageMethods オブジェクトを使用して、asp.net で分離コード メソッドを呼び出します。string、int などのパラメーターを送受信できます。ただし、javaScript オブジェクトを codeBehind に送信する必要があります。パラメータを解析してコードビハインドからデータを取得するにはどうすればよいですか? JSONパーサーを使用する必要があると思いますが、簡単な方法があるかどうか、または.netフレームワークにJsonパーサー(またはJSONのような)があるかどうか疑問に思いますか?

    <script type="text/javascript" language="javascript">
        function test(idParam, nameParam) {
            var jsonObj = { id: idParam, name: nameParam };

            PageMethods.testMethod(jsonObj,
                function (result) { alert(result) });
        }
    </script>
    [WebMethod()]
    public static string testMethod(object param)
    {
        int id = 1;//I must parse param and get id
        string name = "hakan"; //I must parse param and get name

        return "Id:" + id + "\n" + "Name:" + name + "\n" + "Date:" + DateTime.Now;
    }
4

1 に答える 1

1

これを試してください(System.Collections.Genericをusing句として追加してさらにクリーンアップできます):

[WebMethod()]
public static string testMethod(object param)
{
    System.Collections.Generic.Dictionary<String, Object> Collection;
    Collection = param as System.Collections.Generic.Dictionary<String, Object>;

    int id = (int) Collection["id"];
    string name = Collection["name"] as String;

    return "Id:" + id + "\n" + "Name:" + name + "\n" + "Date:" + DateTime.Now;
}

[編集]さらに簡単:

// using System.Collections.Generic;
[WebMethod()]
public static string testMethod(Dictionary<String, Object> Collection)
{       
    int id = (int) Collection["id"];
    string name = Collection["name"] as String;

    return "Id:" + id + "\n" + "Name:" + name + "\n" + "Date:" + DateTime.Now;
}
于 2012-10-15T13:28:53.693 に答える