GridPanel绑定后台数据同时分页demo

前台页面:

 

<head>
    <title>绑定后台数据</title>
    <script type="text/javascript" src="ext-3.1.1/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="ext-3.1.1/ext-all.js"></script>
    <link href="ext-3.1.1/resources/css/ext-all.css" rel="Stylesheet" />
    <script type="text/javascript">
        Ext.onReady(function() {
            //begin

            //创建列
        var cm = new Ext.grid.ColumnModel(

        [
      
            { header: "用户ID", name: "ID", sortable: true },
            { header: "用户名", name: "UserName", sortable: true },
            { header: "年龄", name: "Age", sortable: true }
            ]);
            //创建recode格式
            var recode = new Ext.data.Record.create([
            { name: "ID" },
            { name: "UserName" },
            { name: "Age" }
            ]);
            //创建JSONreader
            var reader = new Ext.data.JsonReader({
            totalProperty: "count", //   该属性是指定记录集的总数(可选的)the property which contains the total dataset size (optional)
                root: "root"
            }, recode);

            //创建store返回recode
            var store = new Ext.data.Store({
                url: "GetUserDataJson.aspx",//通过访问服务端的数据
                reader: reader
            })
      
          store.load({params:{ start: 0, limit: 3}});//传递参数

            var gridpanel = new Ext.grid.GridPanel({
                title: "通过后台获取数据",
                height: 500,
                store: store,
                cm: cm,
                bbar: new Ext.PagingToolbar({pageSize:3,store:store,displayInfo:true,displayMsg:"显示第{0}~{1},共有{2}条记录",emptyMsg:"没有可显示的数据"}), //创建分页
                renderTo: "gpanel"

            });
            //end


        })
    </script>
</head>
<body>
<div id="gpanel"></div>
</body>

 

后台页面:

 

<%
    int start =int.Parse(Request.Params["start"].ToString());
    int limit = int.Parse(Request.Params["limit"].ToString());
   
   
    string sqlcon = ConfigurationManager.ConnectionStrings["mycon"].ConnectionString;
    using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(sqlcon))
    {

        string sql = string.Format("select top {0} * from tuser where id not in( select top {1} id from tuser order by id desc) order by id desc",limit,start);
        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con))
        {
            con.Open();
            System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
            StringBuilder allStr = new StringBuilder();
            allStr.Append(@"{root:[");
            int i = 0;
            while (reader.Read())
            {
                i++;

                allStr.Append("{ID:'" + reader["Id"].ToString() + "',UserName:'" + reader["UserName"].ToString() + " ',Age:'" + reader["age"].ToString() + "'},");

            }
            allStr = allStr.Remove(allStr.Length - 1, 1);
            allStr.Append(@"],count:'10'}");
           %>
           <%=allStr.ToString() %>
          
           <%

        }
    }
   
     %>

 

你可能感兴趣的:(gridPanel)