Jquery+ashx+json 绑定数据

1 篇文章 0 订阅

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 
        {

        }
    }

“` 小白一枚 有很多不足之处请指出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值