[html][easyui]DataGrid 绑定

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Basic CRUD Application - jQuery EasyUI CRUD Demo</title>
    <link rel="stylesheet" type="text/css" href="js/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="js/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="js/themes/color.css">
    <link rel="stylesheet" type="text/css" href="js/demo/demo.css">

    <script type="text/javascript" src="js/jquery.min.js"></script>

    <script type="text/javascript" src="js/jquery.easyui.min.js"></script>

</head>
<body>
    <h2>
        Basic CRUD Application</h2>
    <p>
        Click the buttons on datagrid toolbar to do crud actions.</p>
    <table id="dg" title="My Users" class="easyui-datagrid" style="width: 700px; height: 250px"
        url="JsonHandler.ashx?type=data" toolbar="#toolbar" pagination="true" rownumbers="true"
        fitcolumns="true" singleselect="true">
        <thead>
            <tr>
                <th field="firstname" width="50">
                    First Name
                </th>
                <th field="lastname" width="50">
                    Last Name
                </th>
                <th field="phone" width="50">
                    Phone
                </th>
                <th field="email" width="50">
                    Email
                </th>
            </tr>
        </thead>
    </table>
    <div id="toolbar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true"
            onclick="newUser()">New User</a> <a href="javascript:void(0)" class="easyui-linkbutton"
                iconcls="icon-edit" plain="true" onclick="editUser()">Edit User</a> <a href="javascript:void(0)"
                    class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="destroyUser()">
                    Remove User</a>
    </div>
    <div id="dlg" class="easyui-dialog" style="width: 400px; height: 280px; padding: 10px 20px"
        closed="true" buttons="#dlg-buttons">
        <div class="ftitle">
            User Information</div>
        <form id="fm" method="post" novalidate>
        <div class="fitem">
            <label>
                First Name:</label>
            <input name="firstname" class="easyui-textbox" required="true">
        </div>
        <div class="fitem">
            <label>
                Last Name:</label>
            <input name="lastname" class="easyui-textbox" required="true">
        </div>
        <div class="fitem">
            <label>
                Phone:</label>
            <input name="phone" class="easyui-textbox">
        </div>
        <div class="fitem">
            <label>
                Email:</label>
            <input name="email" class="easyui-textbox" validtype="email">
        </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconcls="icon-ok" onclick="saveUser()"
            style="width: 90px">Save</a> <a href="javascript:void(0)" class="easyui-linkbutton"
                iconcls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width: 90px">
                Cancel</a>
    </div>

    <script type="text/javascript">
        var url;
        function newUser(){
            $('#dlg').dialog('open').dialog('center').dialog('setTitle','New User');
            $('#fm').form('clear');
            url = 'JsonHandler.ashx?type=add';
        }
        function editUser(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $('#dlg').dialog('open').dialog('center').dialog('setTitle','Edit User');
                $('#fm').form('load',row);
                url = 'JsonHandler.ashx?type=edit&id='+row.id;
            }
        }
        function saveUser(){
            $('#fm').form('submit',{
                url: url,
                onSubmit: function(){
                    return $(this).form('validate');
                },
                success: function(result){
                    var result = eval('('+result+')');
                    if (result.errorMsg){
                        $.messager.show({
                            title: 'Error',
                            msg: result.errorMsg
                        });
                    } else {
                        $('#dlg').dialog('close');        // close the dialog
                        $('#dg').datagrid('reload');    // reload the user data
                    }
                }
            });
        }
        function destroyUser(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $.messager.confirm('Confirm','Are you sure you want to destroy this user?',function(r){
                    if (r){
                        $.post('JsonHandler.ashx',{type:'del',id:row.id,random:Math.random()},function(re){
                            var result = eval("("+re+")");
                            if (result.success){
                                alert('');
                                $('#dg').datagrid('reload');    // reload the user data
                            } else {
                                $.messager.show({    // show error message
                                    title: 'Error',
                                    msg: result.errorMsg
                                });
                            }
                        },'json');
                    }
                });
            }
        }
    </script>

    <style type="text/css">
        #fm
        {
            margin: 0;
            padding: 10px 30px;
        }
        .ftitle
        {
            font-size: 14px;
            font-weight: bold;
            padding: 5px 0;
            margin-bottom: 10px;
            border-bottom: 1px solid #ccc;
        }
        .fitem
        {
            margin-bottom: 5px;
        }
        .fitem label
        {
            display: inline-block;
            width: 80px;
        }
        .fitem input
        {
            width: 160px;
        }
    </style>
</body>
</html>
using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Newtonsoft.Json;

namespace Web
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class JsonHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Request.Params != null && context.Request.Params.Count > 0)
            {
                string type = context.Request.Params["type"];
                if (!string.IsNullOrEmpty(type))
                {
                    if (type == "del")
                    {
                        // do something
                        context.Response.Write(JsonConvert.SerializeObject("{'success':'true'}"));
                    }
                    else if (type == "data")
                    {
                        // 根据参数 page / rows / offset 返回相应数据
                        DataTable dt = new DataTable();
                        dt.Columns.Add("firstname");
                        dt.Columns.Add("lastname");
                        dt.Columns.Add("phone");
                        dt.Columns.Add("email");
                        dt.Columns.Add("id");
                        DataRow dr = dt.NewRow();
                        dr[0] = "X";
                        dr["id"] = 0;
                        dt.Rows.Add(dr);
                        dr = dt.NewRow();
                        dr[1] = "Y";
                        dr["id"] = 1;
                        dt.Rows.Add(dr);

                        context.Response.Write(JsonConvert.SerializeObject(dt));
                    }
                    else if (type == "add")
                    {
                        // 执行 SQL 语句增加行到 DataTable
                        context.Response.Write("\"{'success':'true'}\"");
                    }
                    else if (type == "edit")
                    {
                        // 执行 SQL 语句修改 DataTable
                        context.Response.Write("\"{'success':'true'}\"");
                    }
                    else
                    {
                        // 输出错误
                        context.Response.Write(JsonConvert.SerializeObject("{'errorMsg':'没有相关参数!'}"));
                    }
                }
                else
                {
                    // 输出错误
                    context.Response.Write("\"{'errorMsg':'没有参数!'}\"");
                }
            }
            else
            {
                // 输出错误
                context.Response.Write("\"{'errorMsg':'没有请求参数!'}\"");
            }
        }

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

 

posted on 2016-01-13 00:52  z5337  阅读(654)  评论(0编辑  收藏  举报