关于C#前后台搭配使用BootGrid控件,调用数据库并返回值

来源:互联网 发布:淘宝妈妈装店铺推荐 编辑:程序博客网 时间:2024/06/04 00:53

本文主要围绕关于C#前后台搭配使用BootGrid控件,调用数据库并返回值。

HTML5代码
设置元素参数和内容,并指定元素ID

 <div class="grid-body">     <table id="tbDocList" data-toggle="bootgrid">      <thead>        <tr>          <th data-column-id="DocID">ID</th>          <th data-column-id="DocName">文件名</th>        </tr>       </thead>      </table></div>

JS代码
页面中的ID与JS中ID匹配

$('#tbDocList').bootgrid({                selection: true,                multiSelect: true,                rowSelect: true,                keepSelection: false,                ajax: true,                ajaxSettings: {                    method: 'GET',                    cache: false                },                post: function () {                    return {                        action: 'GetDocList',                        ID: document.getElementById("ID").value                    };                },                url: '../../路径A/类B.ashx',                rowSize: 10,                rowCount: [10],                labels: {                 all: "所有",                 //修改为空内容。                 infos: "显示{{ctx.start}}-{{ctx.end}}条,共{{ctx.total}}条",                 loading: "载入中。。。",                 refresh: "刷新",                 noResults: "无记录!"                },                responseHandler: function (response) {                    if (response.IsSuccess) {                        return response.Data;                    } else {                        alert(response.Message);                        return null;                    }                },            });

———————————————————我是后台——————————————————

后台代码中“GetDocList”需要与前台bootgrid使用ajax传输过来的参数名称一致

public void ProcessRequest(HttpContext context)        {                    string action = context.Request["action"].ToString();            bool result = false;            i = 1;            switch (action)            {                case "GetDocList":                page =int.Parse(context.Request.QueryString["current"]);                dt = GetDocList(context);                break;                default:                    context.Response.ContentType = "text/plain";                    context.Response.Write("Hello World");                    break;            }            context.Response.ContentType = "text/plain";            var list = dt.AsEnumerable();            var datas = list.Skip((page - 1) * 10)                .Take(10)                .Select(dr => new                {                    NO = (page - 1) * 10 + i++,                    DocID = dr.Field<int>("DocID"),                    DocName = dr.Field<string>("DocName")                });            var data = new            {                current = page,                rowCount = 10,                rows = datas,                total = list.Count()            };            context.Response.Write(AjaxResult.Success(data));        }

这里后台就会调用GetDocList方法

这里做了一个代码处理

 private DataTable GetDocList(HttpContext context)        {            int flag = int.Parse(context.Request["flag"]);            DataTable DocInfo =  new DataTable();            DocInfo = Initial(context);            return DocInfo;        } public DataTable Initial(HttpContext context)         {            string ID = context.Request["DocName"].ToString();            DBAccess oDBA = new DBAccess();            SqlParameter[] param = new SqlParameter[1];            param[0] = new SqlParameter("@ID", SqlDbType.Int);            param[0].Value = int.Parse(ID);            //传入参数到存储过程,并返回table            DataSet ds = new DataSet();            ds = oDBA.CallSP("USP_GetDocInfo", param);            DataTable DocInfo = ds.Tables[0];            return DocInfo;        }

oDBA.CallSP 这个调用存储过程的方法请参考“如何使用C#连接SQLServer

0 0
原创粉丝点击