jquery 前台创建一个异步对象《后期会用javascript做异步》
function bind_ddldept() {
$.ajax({
url: "../Handle/Department.ashx",//向服务器请求的地址
type: "get",//请求方式《post/get》
data: {
operateType: "bind_ddldepart"//后面可接多个
},
success: function (data) {
//取出后台生成的json数据 循环添加<select></select>的<option>项
$.each(data, function (index, o) {
$("#ddldept").append(" <option value='"+o.DeptID+"'>"+o.DeptName+"</option>");
})
}
})
添加列表数据也是一样 也是追加到 table 下面的数据行
.ashx 后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Script.Serialization;
using System.Text;
namespace UIL.Handle
{
///
/// epartment 的摘要说明
///
public class epartment : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/json";
if (!string.IsNullOrEmpty(context.Request["operateType"]))
{
#region 绑定下拉框
if (context.Request["operateType"] == "bind_ddldepart")
{
DataTable dt = BLL.UserInfo.getlist("Department a left join userinfo b on a.ManagerID=b.UserID", "");
//将datatable中的数据循环添加到泛型集合
List<Model.Department> list = new List<Model.Department>();
foreach (DataRow dr in dt.Rows)
{
Model.Department d = new Model.Department();
d.DeptId = dr["DeptID"] as int ;
d.DeptInfo = dr["DeptInfo"] as string;
d.DeptName = dr["DeptName"] as string;
d.ManagerID = dr["ManagerID"] as string ;
list.Add(d);
}
//序列化json 的方式有很多 JavaScriptSerializer 、DataContractJsonSerializer 等
string json = new JavaScriptSerializer().Serialize(list);
context.Response.Write(json);
}
#endregion
}
else
{
}
}
“` 小白一枚 有很多不足之处请指出