码迷,mamicode.com
首页 > Web开发 > 详细

jquery和ashx搭配使用

时间:2016-08-30 18:00:00      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

//js搭配ashx的使用
$(function(){    //文档一加载完成就要运行,$(function()){} 相当于$(document).ready(function(){});
    $(‘#btnadd‘).on(‘click‘,function(){
        $.post(‘/admin/ashx/part.ashx‘,{"action":"getall"},function(msg){
            var model=JSON.parse(msg);
            $(‘#partcode‘).append($(‘<option value="0">请选择上级类别</option>‘));
            var arr=model.data;
            if(arr.length>0)
            {
                $.each(arr,function(i,item){
                    $(‘#patrcode‘).append($(‘<option value="‘+item.code+‘">‘+item.remark+‘</option>‘));
                });
            }
        });        
    });
});
 function add(){
     var remark=$(‘#remark‘).val();
     var partcode=$(‘#partcode‘).val();
     var data={‘partcode‘:partcode,‘remark‘:remark,"action":"add"};
     $.post(‘/admin/ashx/part.ashx‘,data,function(msg){
         if(msg=="ok"){
             $(‘.close‘).trigger(‘click‘);
             layer.open({
                 content:‘添加成功‘,
                 yes:function(){
                     window.location.reload();
                 }
             });
         }
         else{
             layer.alert(msg);
         }
     });
 }

function edit(code){
    var data={"action":edit,‘code‘:code};
    $(‘#hiddencode‘).val(code);
    $.post(‘admin/ashx/part‘,data,function(msg){
        var model=JSON.parse(msg);
        if(model.success){
            $(‘#editcode‘).append($(‘<option value="0">请选择上个级别</option>‘));
            var arr=model.data;
            if(arr.length>0){
                $.each(arr,function(i,item){
                if(item.code==code){}
                else{
                    $(‘editcode‘).append($(‘<option value="‘+item.code+‘">‘+item.remark+‘</option>‘));
                }
            });
            $(‘#editcode‘).attr(‘value‘,model.editcode);
            }
            $(‘#editremark‘).val(model.remark);
            if(model.iswrong==0){
                $(‘#editiswrong0‘).attr(‘checked‘,true);
            }
            else{
                $(‘#editiswrong1‘).attr(‘checked‘,true);
            }
            
        }
        else{
            $(‘.close‘).trigger(‘click‘);
            window.location.reload();
            layer.alert(‘类别不存在‘);
        }
    });
}

 

 

using BLL;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.admin.ashx
{
    /// <summary>
    /// partypemgr 的摘要说明
    /// </summary>
    public class partypemgr : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string msg = "";
            PartypeBLL bll = new PartypeBLL();
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            #region 所有父类
            if(action=="getall")
            {
                var all = bll.LoadEntities(t=>t.iswrong==0&&t.parentcode=="0").ToList();
                context.Response.Write(JsonConvert.SerializeObject(new { data=all }));
            }
            #endregion
            #region 添加

            if (action == "add")//添加
            {
                string remark = context.Request["remark"];
               // int iswrong = int.Parse(context.Request["iswrong"]);
                string parentcode = context.Request["parentcode"];

                if (string.IsNullOrEmpty(remark))
                {
                    msg = "类别名称不能为空";
                }

                if (string.IsNullOrEmpty(msg))
                {
                    Partype model = new Partype();
                    model.typecode = Guid.NewGuid().ToString();
                    model.remark = remark;
                    model.iswrong = 0;
                    model.parentcode = parentcode;
                    if (bll.LoadEntities(g => g.remark == remark).FirstOrDefault() != null)
                    {
                        msg = "类别名称已经存在";
                    }
                    else
                    {
                        if (bll.AddEntity(model))
                        {
                            msg = "OK";
                        }
                        else
                        {
                            msg = "添加失败";
                        }
                    }
                }
                context.Response.Write(msg);
            }
            #endregion
            #region 删除
            if (action == "del")
            {
                string typecode = context.Request["typecode"];
                Partype model = bll.LoadEntities(g => g.typecode == typecode).FirstOrDefault();
                if (model != null)
                {
                    if (bll.DelEntity(model))
                    {
                        msg = "OK";
                    }
                    else
                    {
                        msg = "删除失败";
                    }
                }
                else
                {
                    msg = "已经删除";
                }
                context.Response.Write(msg);
            }
            #endregion
            #region 编辑
            if (action == "edit")
            {
                string typecode = context.Request["typecode"];
                Partype model = bll.LoadEntities(g => g.typecode == typecode).FirstOrDefault();
                if (model != null)
                {
                    var all = bll.LoadEntities(t => t.iswrong == 0 && t.parentcode == "0").ToList();
                    context.Response.Write(JsonConvert.SerializeObject(new { success = true, remark = model.remark, iswrong = model.iswrong, parentcode = model.parentcode,data=all }));
                }
                else
                {
                    context.Response.Write(JsonConvert.SerializeObject(new { success = false }));
                }
            }
            #endregion
            #region 更新
            if (action == "update")
            {
                string typecode = context.Request["typecode"];
                string remark = context.Request["remark"];
                string parentcode = context.Request["parentcode"];
                int iswrong = int.Parse(context.Request["iswrong"]);
                Partype model = bll.LoadEntities(g => g.typecode==typecode).FirstOrDefault();
                if (model != null)
                {
                    model.remark = remark;
                    model.iswrong = iswrong;
                    model.parentcode = parentcode;
                    if (bll.UpdateEntity(model))
                    {
                        context.Response.Write(JsonConvert.SerializeObject(new { success = true }));
                    }
                    else
                    {
                        context.Response.Write(JsonConvert.SerializeObject(new { success = false }));
                    }
                }
                else
                {
                    context.Response.Write(JsonConvert.SerializeObject(new { success = false }));
                }
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

jquery和ashx搭配使用

标签:

原文地址:http://www.cnblogs.com/hello1688/p/5822639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!