![](/img/trans.png)
[英]How to pass a Global String Variable from the C# code behind of an ASP.NET page to a classic ASP page?
[英]How to pass Dictionary<string, string> variable from code behind to asp.net?
我試圖將公共字典從c#傳遞到asp.net。
這是我的代碼:
代碼背后:
public Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RetrieveDataField();
}
}
void RetrieveDataField()
{
DataDefinitionResponse[] _dr = _service.DataDefinitionList(_companyID);
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Type");
foreach (DataDefinitionResponse dr in _dr)
{
if (dr.Type != "Group" && dr.Type != "File")
{
DataRow row = dt.NewRow();
row["Name"] = dr.Name;
row["Type"] = dr.Type;
dt.Rows.Add(row);
if (dr.Type == "Dropdown")
{
string[] strSplit = dr.ListValue.Split('|');
List<string> lst = new List<string>();
foreach (string word in strSplit)
{
lst.Add(word);
}
dict.Add(dr.Name, lst);
}
}
}
ddlFieldName.DataSource = dt;
ddlFieldName.DataTextField = "Name";
ddlFieldName.DataValueField = "Type";
ddlFieldName.DataBind();
}
ASP:
<script type="text/javascript">
$(document).ready(function () {
alert(<%=dict%>); //Error here
$("#MainContent_ddlFieldName").live("change", function () {
$.ajax({
type: "POST",
url: "WebService/WebFunction.asmx/PopulateDropdown",
data: "{'dict': '" + 'a' + "'}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
// data.d;
// $("#txtBLShipperContactNo").val(data.d);
alert(data.d);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error Shipper: " + errorThrown);
}
});
});
});
</script>
有什么想法嗎?
當你使用<%=dict%>
,編譯器正在執行默認的ToString()方法,所以你得到類似alert(System.Collections.Generic.Dictionary...);
。 請注意,此處沒有引號,因此會生成javascript錯誤。 您可以通過在引號中包圍它來“修復”這個: alert("<%=dict%>");
。
但是,這可能不是你想要做的。 您很可能正在嘗試將實際字典放入Javascript對象中。 為此,您可以使用javascript的JSON.parse
和System.Web.Script.Serialization.JavaScriptSerializer
將數據輸出為JSON對象。 如果您正在使用jQuery,那么您可以使用parseJSON,否則您可以在大多數瀏覽器上使用JSON.parse,我認為(我總是使用jQuery,所以我比瀏覽器提供的JSON.parse更熟悉它)。
以下是將對象輸出到JSON的一些代碼:
public string DictJSON
{
get {
JavaScriptSerializer jSer = new JavaScriptSerializer();
return jSer.Serialize(dict);
}
}
以下是使用jQuery的方式:
$.parseJSON('<%=DictJSON %>');
請注意,JavaScriptSerializer是System.Web.Extensions.dll的一部分,因此您需要添加對它的引用。 它不能處理所有對象類型,但是Dictionary應該沒問題,List也應該如此。 此外,由於它以雙引號輸出JSON對象字符串,因此請確保在parseJSON中使用單引號。
我真的不知道你的代碼產生的錯誤,但這就是我所知道的。 字典應該像這樣添加或“綁定” :
DataTable dt = new DataTable();
Dictionary<string,string> dict = new Dictionary<string,string>();
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Type", typeof(string)));
foreach (DataDefinitionResponse dr in _dr)
{
if (dr.Type != "Group" && dr.Type != "File")
{
DataRow row = dt.NewRow();
row["Name"] = dr["Name"].toString();
row["Type"] = dr["Key"].toString;
dt.Rows.Add(row);
}
}
foreach(DataRow dr in dt.Rows)
{
dict.Add(dr.ItemArray[0].ToString(), dr.ItemArray[1].ToString());
}
ddlFieldName.DataSource = dict; // the dictionary should be here
ddlFieldName.DataTextField = "Key";
ddlFieldName.DataValueField = "Value";
ddlFieldName.DataBind();
或者在第一個foreach循環中添加Dictionary元素,如下所示:
foreach (DataDefinitionResponse dr in _dr)
{
if (dr.Type != "Group" && dr.Type != "File")
{
DataRow row = dt.NewRow();
row["Name"] = dr["Name"].toString();
row["Type"] = dr["Key"].toString();
dt.Rows.Add(row);
dict.Add(dr["Key"].ToString(), dr["Name"].ToString());
}
}
ddlFieldName.DataSource = dict; // the dictionary should be here
ddlFieldName.DataTextField = "Key";
ddlFieldName.DataValueField = "Value";
ddlFieldName.DataBind();
UPDATE
您以錯誤的方式聲明或初始化列。 您可以嘗試我的更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.