ajax 调用及前台拼循环拼写html
asp代码
$(function () {
$("#btnShearch").click(function () {
$.ajax({
type: "Post",
url: "SearchSentence.aspx/getSearchList",
data: "{'Sentence': '" + Sentence + "'," + "'chk_SynonymsUsed': '" + chk_SynonymsUsed + "','chk_UnnecessaryWordsUsed':'" + chk_UnnecessaryWordsUsed + "','MaximumRows':'" + MaximumRows + "'}",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) {
var rsStr = data.d;
// チェックエラーの場合
if (rsStr.substr(0, 4) == ("エラー:")) {
var errArr = rsStr.split(":");
showMessageDialog(errArr[1], errArr[0]);
document.getElementById("content_contentFooter_footer_message").innerText = "XXXX..."
return;
}
data = jQuery.parseJSON(rsStr);
var itemRow = "<table style=\"display: block; overflow-x: auto; overflow-y: auto; overflow: auto \" >";
itemRow += "<tbody>";
$.each(data, function (i, item) {
itemRow += "<tr>";
for (var k in item) {
itemRow += "<td class=\"ell2\" οnclick=cellClick(this)>" + (null == item[k] ? "" : item[k]) + "</td>";
}
itemRow += "</tr>";
})
itemRow += "</tbody>";
itemRow += "</table>";
$("#divItems").html(itemRow);
SetTbBgcolor();
document.getElementById("content_contentFooter_footer_message").innerText = "検索完了しました."
//
document.getElementById("divItemsF").style.display = 'block'
document.getElementById("btnShearch").disabled = false
document.getElementById("btnShearch").focus();
},
error: function (err) {
alert("バックエンド処理が失敗しました。エラーは" + err.responseText + "です。");
}
});
});
});
...
<div id="divItemsF" style="overflow-x:auto; overflow-y:auto">
<table id="divItems" style="table-layout:fixed; min-width: 100%; height: 100%" ></table>
</div>
###############################################################
cs代码
[WebMethod]
public static string getSearchList(string Sentence, bool chk_SynonymsUsed, bool chk_UnnecessaryWordsUsed, string MaximumRows)
{
DataTable searchSentenceDataTable = null;
try
{
// 数据取得
searchSentenceDataTable = dao.getSearchSentenceList(Sentence);
return DataTableToJSON(searchSentenceDataTable);
}
// DataTable转换成JSON格式
[WebMethod]
public static string DataTableToJSON(DataTable table)
{
var list = new List<Dictionary<string, object>>();
foreach (DataRow row in table.Rows)
{
var dict = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
dict[col.ColumnName] = row[col];
}
list.Add(dict);
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
string str = serializer.Serialize(list);
return str;
}
/// Array数组转换成DataTable
public static DataTable ArrayToDataTable(string[,] arr)
{
DataTable dataSouce = new DataTable();
for (int i = 0; i < arr.GetLength(1); i++)
{
DataColumn newColumn = new DataColumn(i.ToString(), arr[0, 0].GetType());
dataSouce.Columns.Add(newColumn);
}
for (int i = 0; i < arr.GetLength(0); i++)
{
DataRow newRow = dataSouce.NewRow();
for (int j = 0; j < arr.GetLength(1); j++)
{
newRow[j.ToString()] = arr[i, j];
}
dataSouce.Rows.Add(newRow);
}
return dataSouce;
}