在asp.net中,jqgrid中没有总页数

时间:2013-02-01 10:45:37

标签: asp.net jqgrid

我制作了jqGrid,但它没有显示网格中的总页数。结果,网格没有移动到下一页(因为它保持为零)。但是,我注意到它正确计算了记录数。

我在下面附上我的代码。

public struct JQGridResults
{
    public int page;
    public int total;
    public int records;
    public JQGridRow[] rows;
}
public struct JQGridRow
{
    public string id;
    public string[] cell;
}
[Serializable]
public class User
{
    public string id { 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 = GetDummyUsers(numberOfRows, pageIndex, sortColumnName, sortOrderBy, out 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.id = user.id;
            row.cell = new string[4];
            row.cell[0] = user.id;
            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> GetDummyUsers(string numberOfRows, string pageIndex, string sortColumnName, string sortOrderBy, out int totalRecords)
    {
        var data = new Collection<User> {
                new User(){ username = "Bill", authcode = "Gates", id= "1", ordinal = "Bill Gates"}
            };
        totalRecords = data.Count;
        return data;
    }
    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 id,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.id = Convert.ToString(dataReader["id"]);
                        user.username = Convert.ToString(dataReader["username"]);
                        user.ordinal = Convert.ToString(dataReader["ordinal"]);
                        user.authcode = Convert.ToString(dataReader["authcode"]);
                        users.Add(user);
                    }
                }
               // totalRecords =(int)(paramTotalRecords.Value);
                 //totalRecords = 0; 

            }

            return users;
        }

    }




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

1 个答案:

答案 0 :(得分:0)

我认为你不小心评论了这一行

// totalRecords =(int)(paramTotalRecords.Value);

请转到

totalRecords =(int)(paramTotalRecords.Value);

已编辑版本

更改

totalRecords =(int)(paramTotalRecords.Value);

totalRecords = users.Count;