jquery和ashx搭配使用

來源:互聯網
上載者:User

標籤:

//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搭配使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.