JQGrid只加载第一页Record而不是前进

时间:2013-02-04 04:36:53

标签: asp.net jqgrid

我正在向jqgrid显示数据,但是我的jqgrid只显示第一页中的100条记录,当我点击下一页按钮图标时,它再次获取相同的记录...而jqgrid正在计算总页数和总数正确记录......下面是我的代码...

的Index.aspx

<script type="text/javascript">
    $(function () {
        $("#UsersGrid").jqGrid({
            url: 'ModCust.ashx',
            datatype: 'json',
            height: 500,
            colNames: ['Duration','username', 'ordinal', 'authcode'],
            colModel: [
                    { name: 'duration', index: 'duration', width: 100, sortable: true,hidden:true },
                    { name: 'username', width: 100, sortable: true },
                    { name: 'ordinal', width: 100, sortable: true },
                    { name: 'authcode', width: 100, sortable: true },

                ],

            cmTemplate: { title: false },
            rowNum: 100,
            rowList: [100, 200, 300],
            pager: '#UsersGridPager',
            sortname: 'duration',
            viewrecords: true,
            autowidth:true,
            sortorder: 'asc'
          });

        $("#UsersGrid").jqGrid('navGrid', '#UsersGridPager', { edit: false, add: false, del: false });
    });

</script>

这是我的处理程序代码..

ModCust.ashx

public struct JQGridResults
{
    public int page;
    public int total;
    public int records;
    public JQGridRow[] rows;
}
public struct JQGridRow
{
    public string duration;
    public string[] cell;
}
[Serializable]
public class User
{
    public string duration { get; set; }
    public string username { get; set; }
    public string ordinal { get; set; }
    public string authcode { get; set; }
}

public class ModCust : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        HttpRequest request = context.Request;
        HttpResponse response = context.Response;

        string _search = request["_search"];
        string numberOfRows = request["rows"];
        string pageIndex = request["page"];
        string sortColumnName = request["sidx"];
        string sortOrderBy = request["sord"];


        int totalRecords;
        Collection<User> users = GetUsers(numberOfRows, pageIndex, sortColumnName, sortOrderBy, out totalRecords);
        string output = BuildJQGridResults(users, Convert.ToInt32(numberOfRows), Convert.ToInt32(pageIndex), Convert.ToInt32(totalRecords));
        response.Write(output);
    }
    private string BuildJQGridResults(Collection<User> users, int numberOfRows, int pageIndex, int totalRecords)
    {

        JQGridResults result = new JQGridResults();
        List<JQGridRow> rows = new List<JQGridRow>();
        foreach (User user in users)
        {
            JQGridRow row = new JQGridRow();
            row.duration = user.duration;
            row.cell = new string[4];
            row.cell[0] = user.duration;
            row.cell[1] = user.username;
            row.cell[2] = user.ordinal;
            row.cell[3] = user.authcode;
            rows.Add(row);
        }
        result.rows = rows.ToArray();
        result.page = pageIndex;
        result.total = (totalRecords + numberOfRows - 1) / numberOfRows;
        result.records = totalRecords;
        JavaScriptSerializer serializer = new JavaScriptSerializer() { MaxJsonLength = Int32.MaxValue, RecursionLimit = 100 };
       // return new JavaScriptSerializer().Serialize(result);
        return serializer.Serialize(result);
    }


    private Collection<User> GetUsers(string numberOfRows, string pageIndex, string sortColumnName, string sortOrderBy, out int totalRecords)
    {
        Collection<User> users = new Collection<User>();
        string connectionString = "Server=localhost;Port=3306;Database=projecttt;UID=root;Pwd=techsoft;pooling=false";

        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            using (MySqlCommand command = new MySqlCommand())
            {
                command.Connection = connection;
                command.CommandText = "select duration,username,ordinal,authcode from processeddata_table ";
                command.CommandType = CommandType.Text; // StoredProcedure;

                MySqlParameter paramPageIndex = new MySqlParameter("@PageIndex", MySqlDbType.Int32);
                paramPageIndex.Value = Convert.ToInt32(pageIndex);
                command.Parameters.Add(paramPageIndex);

                MySqlParameter paramColumnName = new MySqlParameter("@SortColumnName", MySqlDbType.VarChar, 50);
                paramColumnName.Value = sortColumnName;
                command.Parameters.Add(paramColumnName);

                MySqlParameter paramSortorderBy = new MySqlParameter("@SortOrderBy", MySqlDbType.VarChar, 4);
                paramSortorderBy.Value = sortOrderBy;
                command.Parameters.Add(paramSortorderBy);

                MySqlParameter paramNumberOfRows = new MySqlParameter("@NumberOfRows", MySqlDbType.Int32);
                paramNumberOfRows.Value = Convert.ToInt32(numberOfRows);
                command.Parameters.Add(paramNumberOfRows);

                MySqlParameter paramTotalRecords = new MySqlParameter("@TotalRecords", MySqlDbType.Int32);
                totalRecords = 0;
                paramTotalRecords.Value = totalRecords;
                paramTotalRecords.Direction = ParameterDirection.Output;
                command.Parameters.Add(paramTotalRecords);


                connection.Open();

                using (MySqlDataReader dataReader = command.ExecuteReader())
                {
                    User user;
                    while (dataReader.Read())
                    {
                        user = new User();
                        user.duration = Convert.ToString(dataReader["duration"]);
                        user.username = Convert.ToString(dataReader["username"]);
                        user.ordinal = Convert.ToString(dataReader["ordinal"]);
                        user.authcode = Convert.ToString(dataReader["authcode"]);
                        users.Add(user);
                    }
                }
                totalRecords = users.Count;

            }

            return users;
        }

    }



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

Plz先生帮助我。提前谢谢。

2 个答案:

答案 0 :(得分:1)

由于您在网格中将数据类型定义为json。你应该把json阅读器放在你的代码中。将它贴在您的网格中。

jsonReader: {
                    repeatitems: false,
                    root: "rows",
                    page: "page",
                    total: "total",
                    records: "records"
                },

答案 1 :(得分:0)

我认为问题出在您的totalRecords = users.Count;上,看起来users.count会根据您的搜索条件返回列表中的总记录数而不是数据库中的总记录数。您必须执行单独的查询才能获得仅使用搜索条件的总记录,这样您就可以获得总记录。

更新:

在getUsers方法中尝试pageIndex = (pageIndex*numberOfRows)-numberOfRows