jquery和ashx搭配使用

//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;
            }
        }
    }
}
时间: 2024-08-28 01:42:12

jquery和ashx搭配使用的相关文章

jquery访问ashx文件示例

转自原文jquery访问ashx文件示例 .ashx 文件用于写web handler的..ashx文件与.aspx文件类似,可以通过它来调用HttpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程.其实就是带HTML和C#的混合文件. .ashx文件适合产生供浏览器处理的.不需要回发处理的数据格式,例如用于生成动态图片.动态文本等内容.很多需要用到此种处理方式.此文档提供一个简单的调用ashx文件的Demo,并贴出关键文件的源码. 以下为Demo中Login.ashx文

asp.net中js和jquery调用ashx的不同方法分享

代码如下: var xhr = new XMLHttpRequest();            xhr.open("get", 'Controls/gengCart.ashx?CartID=' + input + '&count=' + inp, true);            xhr.setRequestHeader("If-Modified-Since", "0");            xhr.onreadystatecha

asp.net 使用JQuery 调用Ashx 后面直接写方法名,通过反射找到对应的方法

using System.Reflection; public class Industry_Manager : IHttpHandler { HttpRequest gRequest = null; HttpContext gContext = null; HttpResponse gResponse = null; string func = string.Empty; string result = string.Empty; string pageUrl = string.Empty;

Jquery调用从ashx文件返回的jsonp格式的数据处理实例

开发环境:vs2010+jquery-1.4.min.js 解决问题:网上代码比较少,好多调试不通,返回数据不用json而用jsonp主要考虑解决跨域问题 开发步骤:打开VS2010,新建一web站点,保存位置选择D:\Website1;添加新项,选择一般处理程序,命名cmdHandler.ashx;添加新项,选择HTML页,命名为testAshx.htm;网上下载jquery-1.4.min.js拷贝到web站点中 项目相关网站源码和运行截图如下: 1.testAshx.htm代码如下: <!

Jquery+ashx实现Ajax

一 Ajax的实现方式 1.使用一般的webform,在页面用jQuery ajax调用,再从取得的html数据中取得<body>内的内容,写入DOM 优点:不用改变现有的asp.net开发模式,可以使用现成的页面:ajax取得的内容是html文本,直接写入DOM即可 缺点:内容浪费,<body>之外的内容都不是必要的,而且如果使用了MasterPage那就... 2.使用一般的webform,但是用Response.Write()控制输出html,在页面用jQuery ajax调

jquery ajax jsonp跨域调用实例代码

今天研究了AJAX使用JSONP进行跨域调用的方法,发现使用GET方式和POST方式都可以进行跨域调用,这里简单分享下,方便需要的朋友 客户端代码 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApp.WebForm1" %><!DOCTYPE html P

Jquery异步

一.Jquery向aspx页面请求数据$("#Button1").bind("click", function () { $.ajax({ type: "post", url: "default.aspx", data: "name=" + $("#Text1").val(), success: function (result) { alert(result.msg); } }); }

HTML+CSS+jQuery 纵向导航 &amp;&amp; 横向导航 &amp;&amp; 消除IE6 BUG &amp;&amp; 感悟怎样学习

<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> <script type="text/javascript" src="jquery-1.11.3.min.js"> </script> <script

avalonjs1.5 入门教程

迷你MVVM框架 avalonjs1.5 入门教程 avalon经过几年以后,已成为国内一个举足轻重的框架.它提供了多种不同的版本,满足不同人群的需要.比如avalon.js支持IE6等老旧浏览器,让许多靠政府项目或对兼容性要求够高的公司也能享受MVVM的乐趣.avalon.modern.js支持IE10以上版本,优先使用新API,性能更优,体积更少.avalon.mobile.js在avalon.modern的基础提供了触屏事件的支持,满足大家在移动开发的需求.此外,它们分别存在avalon.