jQuery Bootgrid排序,分页和搜索功能无法正常工作

时间:2017-03-18 16:24:17

标签: asp.net json ajax generic-handler jquery-bootgrid

我在我的ASP.Net应用程序中实现了一个jQuery bootgrid,它使用Generic Handler填充。

我使用bootgrid填写Generic Handler,如下所示:

$(function () {
    var grid = $("#grid").bootgrid({
        ajax: true,
        ajaxSettings: {
            method: "GET",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false
        },
        url: "/MyHandler.ashx",
        rowCount: [10, 50, 75, 100, 200, -1]
    });
}

此处有MyHandler.ashx代码:

public class RolesHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/json";
        context.Response.Write(GetData());
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public string GetData()
    {
        var result = string.Empty;
        var con = new SqlConnection();
        var cmd = new SqlCommand();
        var dt = new DataTable();
        string sSQL = @"SELECT Id, Name
                        FROM dbo.AspNetRoles;";

        try
        {
            using (var connection = THF.Models.SQLConnectionManager.GetConnection())
            {
                using (var command = new SqlCommand(sSQL, connection))
                {
                    connection.Open();
                    command.CommandTimeout = 0;
                    var da = new SqlDataAdapter(command);
                    da.Fill(dt);
                }
            }

            var sNumRows = dt.Rows.Count.ToString();
            var sDT = JsonConvert.SerializeObject(dt);
            result = "{ \"current\": 1, \"rowCount\": 10, \"rows\": " + sDT + ", \"total\": " + sNumRows + " }";
        }
        catch (Exception ex)
        {
        }
        finally
        {
            cmd.Dispose();
            THF.Models.SQLConnectionManager.CloseConn(con);
        }

        return result;
    }
}

基本上,在我以ajax方式实现之前,我的bootgrid的所有重要功能都无法正常工作。具体而言,orderingsearchingpagination功能完全没有任何错误。

据我所知,从一些研究中可以看出来。这是因为每次创建搜索短语,或者单击标题(用于排序)等。开机网格执行ajax调用。

有关如何修复此功能的想法吗?

1 个答案:

答案 0 :(得分:4)

经过大量工作后,我最终得到了它,这是最终的代码结果:

public class RolesHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/json";

        var current = context.Request.Params["current"];
        var rowCount = context.Request.Params["rowCount"];
        var orderById = context.Request.Params["sort[Id]"];
        var orderByName = context.Request.Params["sort[Name]"];
        var searchPhrase = context.Request.Params["searchPhrase"];

        var orderBy = "Id";
        var orderFrom = "ASC";

        if (orderById != null)
        {
            orderBy = "Id";
            orderFrom = orderById;
        }
        else if (orderByName != null)
        {
            orderBy = "Name";
            orderFrom = orderByName;
        }

        context.Response.Write(GetData(current, rowCount, orderBy, orderFrom, searchPhrase));
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public string GetData(string current, string rowCount, string orderBy, string orderFrom, string searchPhrase)
    {
        var result = string.Empty;

        var currentNum = Convert.ToInt32(current) - 1;
        var temp = 0;
        if (!"Id".Equals(orderBy, StringComparison.OrdinalIgnoreCase)
            && !"Name".Equals(orderBy, StringComparison.OrdinalIgnoreCase))
            throw new ArgumentException("orderBy is not a valid value");
        if (!"desc".Equals(orderFrom, StringComparison.OrdinalIgnoreCase) && !"asc".Equals(orderFrom, StringComparison.OrdinalIgnoreCase))
            throw new ArgumentException("orderFrom is not a valid value");
        if (!int.TryParse(rowCount, out temp))
            throw new ArgumentException("Rowcount is not a valid number");

        var dt = new DataTable();
        string sSQL = @"SELECT Id, Name
                        FROM dbo.AspNetRoles
                        WHERE Id LIKE @searchPhrase
                            OR Name LIKE @searchPhrase
                        ORDER BY " + orderBy + " " + orderFrom + @"
                        OFFSET ((" + currentNum.ToString() + ") * " + rowCount + @") ROWS
                        FETCH NEXT " + rowCount + " ROWS ONLY;";

        using (var connection = THF.Models.SQLConnectionManager.GetConnection())
        {
            using (var command = new SqlCommand(sSQL, connection))
            {
                command.Parameters.Add(new SqlParameter("@searchPhrase", "%" + searchPhrase + "%"));
                command.Parameters.Add(new SqlParameter("@orderBy", orderBy));

                connection.Open();
                command.CommandTimeout = 0;
                var da = new SqlDataAdapter(command);
                da.Fill(dt);
                connection.Close();
            }
        }

        var total = string.Empty;

        string sSQLTotal = @"SELECT COUNT(*)
                             FROM dbo.Log
                             WHERE Id LIKE @searchPhrase
                                OR Name LIKE @searchPhrase;";

        using (var connection = THF.Models.SQLConnectionManager.GetConnection())
        {
            using (var command = new SqlCommand(sSQLTotal, connection))
            {
                command.Parameters.Add(new SqlParameter("searchPhrase", "%" + searchPhrase + "%"));

                connection.Open();
                command.CommandTimeout = 0;
                total = command.ExecuteScalar().ToString();
                connection.Close();
            }
        }

        var rows = JsonConvert.SerializeObject(dt);

        return result = "{ \"current\": " + current + ", \"rowCount\": " + rowCount + ", \"rows\": " + rows + ", \"total\": " + total + " }";
    }
}