Handler1.ashx
public void ProcessRequest(HttpContext context)
{
context.Response.ClearContent();
context.Response.ContentType = "text/plain";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //无缓存
string UserID = context.Request.Params["UserID"];
string Type = context.Request.Params["Type"];
if (!string.IsNullOrEmpty(UserID)&&!string.IsNullOrEmpty(Type)) {
switch (Type) {
case "Select_lxl":
context.Response.Write(select_lxl(UserID));
break;
}
}
}
private string select_lxl(string UserID) {
StringBuilder JSONstring = new StringBuilder();
JSONstring.Append("[");//这里是开始位置
JSONstring.Append("{");
//已报名
DataSet MyDataSet3 = xh.GetSetByUser(UserID);
if (MyDataSet3 != null && MyDataSet3.Tables[0] != null && MyDataSet3.Tables[0].Rows.Count > 0)
{
DataTable tb = MyDataSet3.Tables[0];
JSONstring.Append("\"yx\"");
JSONstring.Append(":");
JSONstring.Append("[");
for (int i = 0; i < tb.Rows.Count; i++)
{
JSONstring.Append("{");
JSONstring.Append("\"associationName\":\"" + tb.Rows[i]["associationName"] + "\",");
JSONstring.Append("\"type\":\"" + tb.Rows[i]["type"].ToString() + "\",");
JSONstring.Append("\"state\":\"" + tb.Rows[i]["state"].ToString() + "\",");
JSONstring.Append("\"Signupid\":\"" + tb.Rows[i]["Signupid"].ToString() + "\"");
JSONstring.Append("},");
}
JSONstring = new StringBuilder(JSONstring.ToString().TrimEnd(','));
JSONstring.Append("],");
}
//可选兴趣协会
DataSet MyDataSet = xh.GetSetByUserAndType(UserID, "1");
if (MyDataSet != null && MyDataSet.Tables[0] != null && MyDataSet.Tables[0].Rows.Count > 0)
{
DataTable tb = MyDataSet.Tables[0];
JSONstring.Append("\"兴趣协会\"");
JSONstring.Append(":");
JSONstring.Append("[");
for (int i = 0; i < tb.Rows.Count; i++)
{
JSONstring.Append("{");
JSONstring.Append("\"pic\":\"" + tb.Rows[i]["pic"] + "\",");
JSONstring.Append("\"associationID\":\"" + tb.Rows[i]["associationID"].ToString() + "\",");
JSONstring.Append("\"associationName\":\"" + tb.Rows[i]["associationName"].ToString() + "\"");
JSONstring.Append("},");
}
JSONstring = new StringBuilder(JSONstring.ToString().TrimEnd(','));
JSONstring.Append("],");
}
//可选兴趣协会
DataSet MyDataSet2 = xh.GetSetByUserAndType(UserID, "2");
if (MyDataSet2 != null && MyDataSet2.Tables[0] != null && MyDataSet2.Tables[0].Rows.Count > 0)
{
DataTable tb = MyDataSet2.Tables[0];
JSONstring.Append("\"传统项目\"");
JSONstring.Append(":");
JSONstring.Append("[");
for (int i = 0; i < tb.Rows.Count; i++)
{
JSONstring.Append("{");
JSONstring.Append("\"pic\":\"" + tb.Rows[i]["pic"] + "\",");
JSONstring.Append("\"associationID\":\"" + tb.Rows[i]["associationID"].ToString() + "\",");
JSONstring.Append("\"associationName\":\"" + tb.Rows[i]["associationName"].ToString() + "\"");
JSONstring.Append("},");
}
JSONstring = new StringBuilder(JSONstring.ToString().TrimEnd(','));
JSONstring.Append("]");
}
if (JSONstring.ToString().EndsWith(",")) { JSONstring = new StringBuilder(JSONstring.ToString().TrimEnd(',')); }
JSONstring.Append("}");
JSONstring.Append("]");
return JSONstring.ToString();
}
jquery
$.ajax({
url: "ajax/Handler1.ashx?time=" + new Date().getTime(),
contentType: "application/json; charset=utf-8",
data: { UserID: 222, Type: "Select_lxl" },
dataType: "json",
success: function (data) {
//alert(data);
var html_yx = "";
$.each(data, function (index, item) {
$.each(item, function (i, t) {
html_yx = "";
if (i == "yx") {
$.each(t, function (n, t) {
html_yx += "
html_yx += "
";
html_yx += " " + t.associationName + "";
html_yx += "
";html_yx += "
";
html_yx += t.type == "1" ? "" : "";
html_yx += " " + (t.state == "1" ? "审批中 " : "已通过 ") + "";
html_yx += "
";html_yx += "
});
$(".yx").html(html_yx);
}
else {
html_yx += "
html_yx += "
" + i + "
";html_yx += "
$.each(t, function (n, t) {
html_yx += " ";
html_yx += " ";
html_yx += " " + t.associationName + "";
})
html_yx += "
html_yx += "
$(".kxxh").append(html_yx);
}
});
});
},
error: function () {
//alert("错误");
}
});