從 JavaScript 將項目添加到 C# 字典 (Adding items to C# dictionary from JavaScript)


問題描述

從 JavaScript 將項目添加到 C# 字典 (Adding items to C# dictionary from JavaScript)

如果有的話,將 ASP.NET/JavaScript 代碼中的鍵值對添加到 C# 字典的公認方法是什麼?TIA。


參考解法

方法 1:

How I've handled this requirement is to get all the data in a name value pair in javascript, then post this to the server via ajax ...

e.g. loc is your page, methodName being the WebMethod in code behind page, arguments you can populate as

var arguments = '"data":"name1=value1&name2=value2"';

$.ajax({
    type: "POST",
    url: loc + "/" + methodName,
    data: "{" + arguments + "}",
    contentType: "application/json; charset=utf‑8",
    dataType: "json",
    success: onSuccess,
    fail: onFail
});

On your code behind, create a web method e.g

[System.Web.Services.WebMethod(EnableSession = true)]
public static string ItemsForDictionary(string data)
{
    Dictionary<String, String> newDict = ConvertDataToDictionary(data);
}

I use a generic method to convert this data parameter in codebehind to a Dictionary.

private static System.Collections.Generic.Dictionary<String, String> ConvertDataToDictionary(string data)
    {

        char amp = '&';
        string[] nameValuePairs = data.Split(amp);

        System.Collections.Generic.Dictionary<String, String> dict = new System.Collections.Generic.Dictionary<string, string>();

        char eq = '=';

        for (int x = 0; x < nameValuePairs.Length; x++)
        {
            string[] tmp = nameValuePairs[x].Split(eq);
            dict.Add(tmp[0], HttpUtility.UrlDecode(tmp[1]));

        }

        return dict;
    }

Anyways .. hope this gives you the idea ...

方法 2:

You can't do it directly. You'd have to send the data back to the server using a post‑back or ajax call, and then add the data to the Dictionary in the server‑side handler.

We could probably be more helpful if you post some of your code to show what you're actually trying to do.

(by Arets PaeglisJo‑PierreAndrew Cooper)

參考文件

  1. Adding items to C# dictionary from JavaScript (CC BY‑SA 2.5/3.0/4.0)

#ASP.NET #javascript #C#






相關問題

System.Reflection.Assembly.LoadFile 鎖定文件 (System.Reflection.Assembly.LoadFile Locks File)

如何在沒有全局變量的情況下一直保留我的變量? (How can I keep my variable all the time without global variables?)

C# / ASP.NET - Web 應用程序鎖定 (C# / ASP.NET - Web Application locking)

關閉模態對話框窗口後 ASP.NET 刷新父頁面 (ASP.NET Refresh Parent Page after Closing Modal Dialog Window)

無法將 NULL 值傳遞給數據庫 (Unable to pass NULL value to database)

wcf:將用戶名添加到消息頭是否安全? (wcf: adding username to the message header is this secure?)

使用 ASP.Net 教初學者 Web 開發的小項目想法 (Small projects ideas to teach beginners web development using ASP.Net)

SQL Server - 分組、擁有和計數 (SQL Server - Group by, having and count in a mix)

企業庫異常處理應用程序塊和日誌記錄應用程序塊在 ASP.NET 中的正確使用 (Enterprise Library Exception Handling Application Block and Logging Application Block proper use in ASP.NET)

來自proc的asp.net多個結果集:是否有必要將結果映射到類?如果是這樣,怎麼做? (asp.net multiple result set from proc: is it necessary to map results to class? If so, how?)

如何在測試工具中實例化 asp.net 代碼隱藏類? (How can I instantiate an asp.net codebehind class in a test harness?)

Web 窗體用戶控制事件,需要在頁面加載後添加 (Web Form User Control Event, needs to be added after page loads)







留言討論