代码经过精心改良,可以直接使用,具体看注释吧.
1,前台,代码如下:
    
   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LineInfoManagement.aspx.cs" Inherits="LMX.XiAnPeiHua.Web.Admin.UnitManagement.LineInfoManagement" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>线路信息设置</title>
    <script src="/js/jquery-1.7.1.min.js"></script>
    <script src="/js/jquery-easyui-1.4.2/jquery.easyui.min.js"></script>
    <script src="/js/jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"></script>
    <link href="/js/jquery-easyui-1.4.2/themes/metro-blue/easyui.css" rel="stylesheet" />
    <link href="/js/jquery-easyui-1.4.2/themes/icon.css" rel="stylesheet" />
    <link href="/css/style.css" rel="stylesheet" />
    <link href="/css/admin.css" rel="stylesheet" />
    <script type="text/javascript">
        $(document).ready(function () {
            fLoadTable();//加载参数
            $('#tt').datagrid('reload', {
                Method: 'FirstLoad'
            });
            $('#unit_id').combobox({
                onSelect: function (rec) {
                    //alert(rec.ID);
                    //$('#site_id').combobox('reload', location.href + '?type=LoadSite&UnitID=' + rec.ID);
                    var vUrl = '/Admin/UnitManagement/LineInfoManagement.aspx?type=LoadSite&UnitID=' + rec.ID;
                    $('#site_id').combobox({
                        url: vUrl,
                        onLoadSuccess: function () {
                            var data = $('#site_id').combobox('getData');
                            if (data.length > 0) {
                                $('#site_id').combobox('select', data[0].ID);
                            }
                            else {
                                $('#site_id').combobox('clear');
                            }
                        }
                    })
                }
            });
        })
        function fLoadTable() {
            $('#tt').datagrid({
                title: '',
                url: location.href,
                pagination: true,
                pageSize: 20,
                pagePosition: 'top',
                striped: true,
                //singleSelect: true,
                ctrlSelect: true,//在启用多行选择的时候允许使用Ctrl键+鼠标点击的方式进行多选操作。
                rownumbers: true,
                columns: [[
                    { field: 'unit_id', title: 'unit_id', align: 'center', hidden: true },
                    { field: 'site_id', title: 'site_id', align: 'center', hidden: true },
                    { field: 'line_id', title: 'line_id', align: 'center', hidden: true },
                    { field: 'ckey', title: '线路编号', align: 'center' },
                    { field: 'name', title: '线路名称', align: 'center' },
                    { field: 'remark', title: '备注', align: 'center' }
                ]],
                toolbar: [{
                    text: '增加',
                    iconCls: 'icon-add',
                    handler: function () {
                        $('#dlg').dialog('open').dialog('setTitle', '新增线路信息');
                        $('#dlg_form').form('clear');
                        $('#unit_id').combo('readonly', false);    // 禁用只读模式
                        $('#site_id').combo('readonly', false);    // 禁用只读模式
                        $('#unit_id').combobox('reload', '/Admin/UnitManagement/LineInfoManagement.aspx?type=LoadUnit');
                        vUrl = '/Admin/UnitManagement/LineInfoManagement.aspx?type=add';
                    }
                }, {
                    text: '修改',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var row = $('#tt').datagrid('getSelected');
                        if (row) {
                            $('#dlg').dialog('open').dialog('setTitle', '修改线路信息');
                            $('#unit_id').combobox('reload', '/Admin/UnitManagement/LineInfoManagement.aspx??type=LoadUnit');
                            $('#site_id').combobox('reload', '/Admin/UnitManagement/LineInfoManagement.aspx?type=LoadSite');
                            $('#dlg_form').form('load', row);
                            $('#unit_id').val(row.unit_id);
                            $('#site_id').val(row.line_id);
                            $('#ckeyPre').val(row.site_id);
                            $('#unit_id').combo('readonly', true);    // 启用只读模式
                            $('#site_id').combo('readonly', true);    // 启用只读模式
                            vUrl = '/Admin/UnitManagement/LineInfoManagement.aspx?type=modify&line_id=' + row.line_id;
                        } else {
                            $.messager.alert('请至少选中一项作为修改项.')
                        }
                    }
                }, '-', {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        var row = $('#tt').datagrid('getSelected');
                        if (row) {
                            $.messager.confirm('提示', '您确定要删除这个线路信息吗?', function (r) {
                                if (r) {
                                    vUrl = '/Admin/UnitManagement/LineInfoManagement.aspx?type=del&site_id=' + row.site_id + '&unit_id=' + row.unit_id + '&line_id=' + row.line_id;
                                    $.ajax({
                                        type: "POST",
                                        url: vUrl,
                                        success: function (json) {
                                            var json = eval('(' + json + ')');
                                            if (json.result == true) {
                                                $.messager.show({
                                                    title: 'Success',
                                                    msg: '删除成功!'
                                                });
                                                $('#dlg').dialog('close');
                                                // close the dialog
                                                $('#tt').datagrid('reload'); // reload the user data
                                            } else {
                                                $.messager.show({
                                                    title: 'Error',
                                                    msg: '删除失败,请稍后再试!'
                                                });
                                                $('#dlg').dialog('close');
                                                // close the dialog
                                                $('#tt').datagrid('reload'); // reload the user data
                                            }
                                        }
                                    });
                                }
                            });
                        }
                        else {
                           $.messager.alert("请至少选中一项作为删除项");

                        }
                    }
                }]
            });
        };
        function fSaveData() {
            $('#dlg_form').form('submit', {
                url: vUrl,
                onSubmit: function () {
                    return $(this).form('validate');
                },
                success: function (sjson) {
                    var json = eval('(' + sjson + ')');
                    if (json.result == true) {
                        $.messager.show({
                            title: 'Success',
                            msg: json.msg ? json.msg : '操作成功!'
                        });
                        $('#dlg').dialog('close');     // close the dialog
                        $('#tt').datagrid('reload'); // reload the user data
                    } else {
                        $.messager.show({
                            title: 'Error',
                            msg: json.msg ? json.msg : '操作失败,请稍后再试!'
                        });
                        $('#dlg').dialog('close');     // close the dialog
                    }
                },
                error: function () {
                    $.messager.show({
                        title: 'Error',
                        msg: json.msg ? json.msg : '操作失败,请稍后再试!'
                    });
                    $('#dlg').dialog('close');     // close the dialog
                }
            });
        }
    </script>
</head>
<body>
    <div class="divForTable">
        <table id="tt" style="width: 100%; height: 600px; $(this).width() * 0.2;">
        </table>
    </div>
    <div id="dlg" class="easyui-dialog" style="width: 300px; height: 350px; padding: 5px 10px"
        data-options="closed:true,buttons:'#dlg-buttons',modal:true">
        <form id="dlg_form" method="post">
            <div class="fitem">
                <label>所属电网:</label>
                <input id="unit_id" name="unit_id" class="easyui-combobox" required="true" data-options="valueField:'ID',textField:'Name',editable:false" />
            </div>
            <div class="fitem">
                <label>所属变电所:</label>
                <input id="site_id" name="site_id" class="easyui-combobox" required="true" data-options="valueField:'ID',textField:'Name',editable:false" />
            </div>
            <%-- <div class="fitem">
                <label>线路编号:</label>
                <input name="ckeyPre" id="ckeyPre" class="easyui-validatebox" readonly="true" style="width: 30px;" />
            </div>--%>
            <div class="fitem">
                <label>名称:</label>
                <input name="name" id="name" class="easyui-validatebox" required="true" />
            </div>
            <div class="fitem">
                <label>备注:</label>
                <input name="remark" id="remark" class="easyui-validatebox" />
            </div>
            <div class="line" style="margin-top: 15px;">
                说明:线路编号由8位数字组成,前5位是所属变电所编号,最后3位代表线路内部编号.(编号在前端不显示,后台自动添加)
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="fSaveData()">保存
        </a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel"
            onclick="javascript:$('#dlg').dialog('close')">取消
        </a>
    </div>
</body>
</html>

2,后台代码如下:
       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["from"] == "LoadF_ParentItemCode")
                {
                    using (item_energyEntities ent = new item_energyEntities())
                    {
                        List<t_dt_energyitemdict> list = (from c in ent.t_dt_energyitemdict where c.F_EnergyItemType == "A" select c).ToList();
                        if (list != null && list.Count != 0)
                        {
                            var vData = from m in list select new { ID = m.F_EnergyItemCode, Name = m.F_EnergyItemName };
                            Response.Write(JsonHelper.Serialize(vData));
                        }
                        else
                        {
                            Response.Write(null);
                        }
                        Response.End();
                    }
                }
                if (Request.Form["Method"] == "FirstLoad")
                {
                    List<t_dt_energyitemdict> list = DataLoader.GetEnergyItemList();
                    int page = string.IsNullOrWhiteSpace(Request["page"]) ? 1 : Int32.Parse(Request["page"]);
                    int rows = string.IsNullOrWhiteSpace(Request["rows"]) ? 1 : Int32.Parse(Request["rows"]);
                    string order = string.IsNullOrWhiteSpace(Request["order"]) ? "ASC" : Request["order"].ToUpper();
                    string sort = string.IsNullOrWhiteSpace(Request["sort"]) ? "ID" : Request["sort"];
                    IQueryable<t_dt_energyitemdict> query = CommonTools.DataSorting<t_dt_energyitemdict>(list.AsQueryable(), sort, order).Skip((page - 1) * rows).Take(rows);
                    Response.Write(JsonHelper.Serialize(new { total = list.Count, rows = query }));
                    Response.End();
                }
                if (Request.QueryString["type"] == "add")
                {
                    string strMsg;
                    if (AddObject(out strMsg))
                    {
                        Response.Write(JsonHelper.Serialize(new { result = true, msg = strMsg }));
                    }
                    else
                    {
                        Response.Write(JsonHelper.Serialize(new { result = false, msg = strMsg }));
                    }
                    Response.End();
                }
                if (Request.QueryString["type"] == "modify")
                {
                    string strMsg;
                    if (ModifyObject(out strMsg))
                    {
                        Response.Write(JsonHelper.Serialize(new { result = true, msg = strMsg }));
                    }
                    else
                    {
                        Response.Write(JsonHelper.Serialize(new { result = false, msg = strMsg }));
                    }
                    Response.End();
                }
                if (Request.QueryString["type"] == "del")
                {
                    string strMsg;
                    if (DelObject(out strMsg))
                    {
                        Response.Write(JsonHelper.Serialize(new { result = true, msg = strMsg }));
                    }
                    else
                    {
                        Response.Write(JsonHelper.Serialize(new { result = false, msg = strMsg }));
                    }
                    Response.End();
                }
            }
        }
        /// <summary>
        /// 增加一个对一个对象.
        /// </summary>
        /// <returns></returns>
        private bool AddObject(out string strMsg)
        {
            string F_EnergyItemCode = Request.Form["F_EnergyItemCode"];
            string F_EnergyItemName = Request.Form["F_EnergyItemName"];
            string F_ParentItemCode = Request.Form["F_ParentItemCode"];
            string F_EnergyItemType = Request.Form["F_EnergyItemType"];
            string F_EnergyItemUnit = Request.Form["F_EnergyItemUnit"];
            string F_EnergyItemFml = Request.Form["F_EnergyItemFml"];
            string F_EnergyItemState = Request.Form["F_EnergyItemState"];
            short sF_EnergyItemState = 0;
            if (short.TryParse(F_EnergyItemState, out sF_EnergyItemState))
            {
                sF_EnergyItemState = short.Parse(F_EnergyItemState);
            }
            bool bPass = false;
            try
            {
                using (item_energyEntities ent = new item_energyEntities())
                {
                    t_dt_energyitemdict aNewModel = new t_dt_energyitemdict()
                    {
                        F_EnergyItemCode = F_EnergyItemCode,
                        F_EnergyItemState = sF_EnergyItemState,
                        F_EnergyItemType = F_EnergyItemType,
                        F_EnergyItemUnit = F_EnergyItemUnit,
                        F_EnergyItemName = F_EnergyItemName,
                        F_EnergyItemFml = F_EnergyItemFml,
                        F_ParentItemCode = F_ParentItemCode
                    };
                    ent.t_dt_energyitemdict.Add(aNewModel);
                    if (ent.SaveChanges() > 0)
                    {
                        bPass = true;
                        strMsg = "增加成功。";
                    }
                    else
                    {
                        bPass = false;
                        strMsg = "增加失败,请稍后再试。";
                    }
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.Message;
                bPass = false;
            }
            return bPass;
        }
        /// <summary>
        /// 更新一个对象.
        /// </summary>
        /// <returns></returns>
        private bool ModifyObject(out string strMsg)
        {
            string F_EnergyItemCode = Request.Form["F_EnergyItemCode"];
            string F_EnergyItemName = Request.Form["F_EnergyItemName"];
            string F_ParentItemCode = Request.Form["F_ParentItemCode"];
            string F_EnergyItemType = Request.Form["F_EnergyItemType"];
            string F_EnergyItemUnit = Request.Form["F_EnergyItemUnit"];
            string F_EnergyItemFml = Request.Form["F_EnergyItemFml"];
            string F_EnergyItemState = Request.Form["F_EnergyItemState"];
            short sF_EnergyItemState = 0;
            if (short.TryParse(F_EnergyItemState, out sF_EnergyItemState))
            {
                sF_EnergyItemState = short.Parse(F_EnergyItemState);
            }
            bool bPass = false;
            try
            {
                using (item_energyEntities ent = new item_energyEntities())
                {
                    t_dt_energyitemdict aModel = (from c in ent.t_dt_energyitemdict where c.F_EnergyItemCode == F_EnergyItemCode select c).FirstOrDefault();
                    if (aModel != null)
                    {
                        aModel.F_EnergyItemCode = F_EnergyItemCode;
                        aModel.F_EnergyItemName = F_EnergyItemName;
                        aModel.F_ParentItemCode = F_ParentItemCode;
                        aModel.F_EnergyItemType = F_EnergyItemType;
                        aModel.F_EnergyItemUnit = F_EnergyItemUnit;
                        aModel.F_EnergyItemFml = F_EnergyItemFml;
                        aModel.F_EnergyItemState = sF_EnergyItemState;
                        if (ent.SaveChanges() > 0)
                        {
                            bPass = true;
                            strMsg = "修改成功。";
                        }
                        else
                        {
                            bPass = false;
                            strMsg = "修改失败,请稍后再试。";
                        }
                    }
                    else
                    {
                        bPass = false;
                        strMsg = "不存在此记录,请稍后再试。";
                    }
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.Message;
                bPass = false;
                //throw;
            }
            return bPass;
        }
        /// <summary>
        /// 删除一个对象.
        /// </summary>
        /// <returns></returns>
        private bool DelObject(out string strMsg)
        {
            string F_EnergyItemCode = Request.QueryString  ["F_EnergyItemCode"];
            bool bPass = false;
            using (item_energyEntities ent = new item_energyEntities())
            {
                t_dt_energyitemdict aModel = (from c in ent.t_dt_energyitemdict where c.F_EnergyItemCode == F_EnergyItemCode select c).FirstOrDefault();
                try
                {
                    if(aModel!=null)
                    {
                        ent.t_dt_energyitemdict.Remove(aModel);
                        if(ent.SaveChanges()>0)
                        {
                            bPass = true;
                            strMsg = "删除成功。";
                        }
                        else
                        {
                            bPass = false;
                            strMsg = "删除失败,请稍后再试。";
                        }
                    }
                    else
                    {
                        bPass = false;
                        strMsg = "不存在此记录,请稍后再试。";
                    }
                }
                catch (Exception ex)
                {
                    bPass = false;
                    strMsg = ex.Message;
                }
            }
            return bPass;

}  

EasyUI DataGrid 实用例子(2015-05-22)的更多相关文章

  1. Easyui DataGrid DateRange Filter 漂亮实用的日期区间段筛选功能

    自定义扩展Jquery easyui datagrid filter组件实现对日期类型区间段的筛选功能.显示效果如一下 是不是非常实用 引用的jquery 组件是 Date Range Picker ...

  2. DataTables VS EasyUI DataGrid 基础应用 转

    DataTables中文网推出了 第一篇 关于DataTables和其他表格插件比较后,为了把让这个比较更有意义,更能帮助到大家,DataTables中文网 做了问卷调查,根据小伙伴们的填写我归纳了一 ...

  3. easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)

    easyui datagrid 禁止选中行   没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) ...

  4. jQuery EasyUI DataGrid Checkbox 数据设定与取值

    纯粹做个记录,以免日后忘记该怎么设定. 这一篇将会说明两种使用 jQuery EasyUI DataGrid 的 Checkbox 设定方式,以及在既有数据下将 checked 为 true 的该笔数 ...

  5. 扩展jquery easyui datagrid编辑单元格

    扩展jquery easyui datagrid编辑单元格 1.随便聊聊 这段时间由于工作上的业务需求,对jquery easyui比较感兴趣,根据比较浅薄的js知识,对jquery easyui中的 ...

  6. EasyUI Datagrid 鼠标悬停显示单元格内容 复制代码

    EasyUI Datagrid 鼠标悬停显示单元格内容 ,halign:, align: 0 « 上一篇:LINQ to Entities 中的查询» 下一篇:去掉字符串中的非数字字符 posted ...

  7. easyui datagrid checkbox multiple columns have been done do

    lengku1987   2013-01-06 22:27:47   Sponsored Links   easyui datagrid checkbox multiple columns have ...

  8. easyui datagrid中 多表头方法总结

    easyui datagrid中怎么设置表头成多行显示呢?其实很简单,就是给datagrid的columns属性设置成多个数组就行了.下面直接看例子吧,这是一个两行表头的,按照此方法,还可以设置三行表 ...

  9. 让easyui datagrid支持bootstrap的tooltip

    让easyui datagrid支持bootstrap的tooltip 发表于 下午 1:53 by ylpro.net & 分类 Java. Easyui在1.3.3版本之前是不支持tool ...

随机推荐

  1. Java_类似java.lang.VerifyError: Expecting a stackmap frame at branch target 22 in method的解决方法

    报异常的方法内使用了Java 7的新特性:自动资源释放,类似于try(){},即在try后面跟一括号,在括号里面对一些资源赋值,try里面的代码块执行完毕之后会自动释放try后面的括号中声明的资源. ...

  2. wex5中的星星评分

    新建一个空白的.w文件,然后在页面上放5个img星星图片 重要的是图片路径不能是绝对路径,要用相对路径,不然js操作的时候会出bug 添加两个label标签(标签随你挑,在这我就用label) 你到时 ...

  3. 关于.9.png格式图片的制作与使用

    .9.png图片其实就是png格式图片,不过它比普通的png图片外围多了1px(像素)的边框,另外就是使用这种格式的图片可以实现背景自适应大小且不失真的效果. 制作使用步骤: 1.制作属于你自己的pn ...

  4. myBatis中 collection 或 association 联合查询 中column 传入多个参数值

    下面是一个树形结构表自连接 联合查询 Demo <resultMap id="BaseResultMap"  type="com.maidan.daas.entit ...

  5. pythonchallenge 解谜 Level 4

    下一关... 一张图片,于是就点击了一下. 跳转到了 http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345 显示的是: ...

  6. ViewPager自动轮播

    Android使用ViewPager实现左右循环滑动及轮播效果   ViewPager是一个常用的android组件,不过通常我们使用ViewPager的时候不能实现左右无限循环滑动,在滑到边界的时候 ...

  7. fwrite写入文件不成功bug

    文件写入了,只是从头覆盖了!因为在fwrite():前面 文件位置指针是是SEEK_SET,即首位置.在fwrite(fileHeader, 1, 10, file); 前面加上一行fseek(fil ...

  8. 在java中如何用键盘输入一个数,字符,字符串

    一,利用 Scanner 实现从键盘读入integer或float 型数据 import java.util.*; public class test { public static void mai ...

  9. CYQ.Data V4系列全面开源(2013-08-04)

    致各位.NET开发者: 考虑到本人开源作品太多,个人维护力度有限,故自2013-08-04开始,开放原本需要授权使用的V4系列的最高版本V4.55的框架源码,同时也开放相对应的辅助工具源码,自此,V4 ...

  10. 尝试解决在构造函数中同步调用Dns.GetHostAddressesAsync()引起的线程死锁

    (最终采用的是方法4) 问题详情见:.NET Core中遇到奇怪的线程死锁问题:内存与线程数不停地增长 看看在 Linux 与 Windows 上发生线程死锁的后果. Linux: Microsoft ...