Ext.Net- GirdPanel进行分页

  分页存储, 这也是一个老生常谈的话题了. 因为工作需要,要接触EXT.NET, 所以今天的劳动是试使用EXT.NET进行分页存储.

  Ext.Net有一个GirdPanel,他有内置的分页存储功能,他内置的分页存储需要先一次性把数据全部取出来的,像SQL语句的select * from 表; 如果数据量小的话,一次性全部取出来再进行分页,那OK,没有任何问题。但是如果你的数据库的记录数达到上万行的时候,这样做需要耗费几秒的时间。单一个GirdPanel的读取就需要好几秒,再加上其他页面加载,用户有时间在一个页面上停留超过10秒吗? 当然NO

  所以使用分页存储过程来进行分页,是很重要的. 好了贴代码

  HTML页面代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test2.aspx.cs" Inherits="YMHExample.test2" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <ext:ResourceManager ID="ResourceManager1" runat="server">
        </ext:ResourceManager>
        <!--GirdPanel内容-->
        <ext:GridPanel ID="GridPanel1" runat="server" TrackMouseOver="true" Title="会议列表"
            Header="false" Icon="Table" AutoHeight="true">
            <Store>
                <ext:Store runat="server" ID="Store1">
                    <Reader>
                        <ext:JsonReader IDProperty="id">
                            <Fields>
                                <ext:RecordField Name="id" />
                                <ext:RecordField Name="name" />
                                <ext:RecordField Name="ftype" />
                                <ext:RecordField Name="contents" />
                                <ext:RecordField Name="fcount" />
                                <ext:RecordField Name="stime" Type="Date" />
                                <ext:RecordField Name="etime" Type="Date" />
                                <ext:RecordField Name="state" />
                                <ext:RecordField Name="addtime" Type="Date" />
                                <ext:RecordField Name="edittime" Type="Date" />
                                <ext:RecordField Name="createname" />
                            </Fields>
                        </ext:JsonReader>
                    </Reader>
                </ext:Store>
            </Store>
            <ColumnModel ID="ColumnModel1" runat="server">
                <Columns>
                    <ext:RowNumbererColumn />
                    <ext:Column Header="状态" Sortable="false" DataIndex="state" Width="60" Align="Center">
                    </ext:Column>
                    <ext:Column Header="会议名称" Sortable="false" DataIndex="name" />
                    <ext:Column Header="会议类型" Sortable="false" DataIndex="ftype" Align="Center" Width="60" />
                    <ext:Column Header="会议内容" Sortable="false" DataIndex="contents" />
                    <ext:Column Header="参会人数" Sortable="false" DataIndex="fcount" />
                    <ext:Column Header="开始时间" Sortable="false" DataIndex="stime" Width="120">
                        <Renderer Fn="Ext.util.Format.dateRenderer('Y-m-d H:i')" />
                    </ext:Column>
                    <ext:Column Header="结束时间" Sortable="false" DataIndex="etime" Width="120">
                        <Renderer Fn="Ext.util.Format.dateRenderer('Y-m-d H:i')" />
                    </ext:Column>
                    <ext:Column Header="创建人" Sortable="false" DataIndex="createname" />
                    <ext:Column Header="添加时间" Sortable="false" DataIndex="addtime" Width="120">
                        <Renderer Fn="Ext.util.Format.dateRenderer('Y-m-d H:i')" />
                    </ext:Column>
                    <ext:Column Header="修改时间" Sortable="false" DataIndex="edittime" Width="120">
                        <Renderer Fn="Ext.util.Format.dateRenderer('Y-m-d H:i')" />
                    </ext:Column>
                </Columns>
            </ColumnModel>
            <SelectionModel>
                <ext:RowSelectionModel runat="server" ID="RowSelectionModel1" SingleSelect="true" />
            </SelectionModel>
            <BottomBar>
                <ext:Toolbar ID="Toolbar2" runat="server">
                    <Items>
                        <ext:Button runat="server" ID="btnFirst" Icon="ControlStartBlue">
                            <DirectEvents>
                                <Click OnEvent="btnFirst_Click">
                                    <EventMask ShowMask="true" Msg="正在翻页..." />
                                </Click>
                            </DirectEvents>
                        </ext:Button>
                        <ext:Button runat="server" ID="btnPrev" Icon="ControlRewindBlue">
                            <DirectEvents>
                                <Click OnEvent="btnPrev_Click">
                                    <EventMask ShowMask="true" Msg="正在翻页..." />
                                </Click>
                            </DirectEvents>
                        </ext:Button>
                        <ext:NumberField runat="server" ID="txtPageIndex" MinValue="1" Width="90" Text="1">
                        </ext:NumberField>
                        <ext:Button runat="server" ID="btnGopage" Icon="ControlRepeatBlue">
                            <DirectEvents>
                                <Click OnEvent="btnGopage_Click">
                                    <EventMask ShowMask="true" Msg="正在翻页..." />
                                </Click>
                            </DirectEvents>
                        </ext:Button>
                        <ext:Button runat="server" ID="btnNext" Icon="ControlFastforwardBlue">
                            <DirectEvents>
                                <Click OnEvent="btnNext_Click">
                                    <EventMask ShowMask="true" Msg="正在翻页..." />
                                </Click>
                            </DirectEvents>
                        </ext:Button>
                        <ext:Button runat="server" ID="btnLast" Icon="ControlEndBlue">
                            <DirectEvents>
                                <Click OnEvent="btnLast_Click">
                                    <EventMask ShowMask="true" Msg="正在翻页..." />
                                </Click>
                            </DirectEvents>
                        </ext:Button>
                        <ext:Label runat="server" ID="lbl_DisplayMsg">
                        </ext:Label>
                    </Items>
                </ext:Toolbar>
            </BottomBar>
        </ext:GridPanel>
    </div>
    </form>
</body>
</html>

  C#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data.SqlClient;
using System.Data;

namespace YMHExample
{
    public partial class test2 : System.Web.UI.Page
    {

        private static int pageCount = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pageCount = GetPangCount() / 10 + 1; //在页面加载的时候 计算出页的总数
                int pageIndex = Convert.ToInt32(txtPageIndex.Text);
                GirdPanelBind("meeting", "*", "id", "asc", 10, pageIndex, "");
                InitParameters();
            }
        }

        /// <summary>
        /// 绑定GirdPanel 分页存储过程
        /// </summary>
        /// <param name="tblName">表名 meeting</param>
        /// <param name="strFields">字段 *</param>
        /// <param name="strOrder">排序字段值 id</param>
        /// <param name="strOrderType">按什么方式排序 asc desc</param>
        /// <param name="PageSize">分页大小</param>
        /// <param name="PageIndex">分页索引</param>
        /// <param name="strWhere">查询条件 无则填空字符串</param>
        protected void GirdPanelBind(string tblName, string strFields, string strOrder, string strOrderType, int PageSize, int PageIndex, string strWhere)
        {
            using (SqlConnection conn = new SqlConnection("server=.;database=YHM;uid=sa;pwd=123456"))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("proc_SplitPage", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] paras = {  
                        new SqlParameter("@tblName",SqlDbType.NVarChar,50), 
                        new SqlParameter("@strFields",SqlDbType.NVarChar,50),
                        new SqlParameter("@strOrder",SqlDbType.NVarChar,50),
                        new SqlParameter("@strOrderType",SqlDbType.NVarChar,50),
                        new SqlParameter("@PageSize",SqlDbType.Int),
                        new SqlParameter("@PageIndex",SqlDbType.Int),
                        new SqlParameter("@strWhere",SqlDbType.NVarChar,50)
                    };

                    paras[0].Value = tblName;
                    paras[1].Value = strFields;
                    paras[2].Value = strOrder;
                    paras[3].Value = strOrderType;
                    paras[4].Value = PageSize;
                    paras[5].Value = PageIndex;
                    paras[6].Value = "";

                    cmd.Parameters.AddRange(paras);
                    DataTable dt = new DataTable();
                    SqlDataReader dr = cmd.ExecuteReader();
                    dt.Load(dr);

                    this.Store1.DataSource = dt;
                    this.Store1.DataBind();
                }
            }
        }

        /// <summary>
        /// 获得总的记录数
        /// </summary>
        protected int GetPangCount()
        {
            using (SqlConnection conn = new SqlConnection("server=.;database=YHM;uid=sa;pwd=123456"))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("select count(*) from meeting", conn))
                {
                    int pageCount = (int)cmd.ExecuteScalar();
                    return pageCount;
                }
            }

        }


        #region 分页
        /// <summary>
        /// 显示处理
        /// </summary>
        private void InitParameters()
        {
            int PageSize = 10;
            lbl_DisplayMsg.Text = "  显示第 " + (((Convert.ToInt32(txtPageIndex.Text) - 1) * 10) + 1) + " 条到 " + (((Convert.ToInt32(txtPageIndex.Text) - 1) * PageSize) + PageSize) + " 条记录,一共 " + pageCount + "";

        }

        /// <summary>
        /// 首页
        /// </summary>
        protected void btnFirst_Click(object sender, DirectEventArgs e)
        {
            txtPageIndex.Text = "1";
            GirdPanelBind("meeting", "*", "id", "asc", 10, 1, "");
        }

        /// <summary>
        /// 下一页
        /// </summary>
        protected void btnNext_Click(object sender, DirectEventArgs e)
        {
            int pageIndex = Convert.ToInt32(txtPageIndex.Text);
            if (pageIndex < pageCount)
            {
                pageIndex++;
                txtPageIndex.Text = pageIndex.ToString();
                GirdPanelBind("meeting", "*", "id", "asc", 10, pageIndex, "");
            }
        }

        /// <summary>
        /// 上一页
        /// </summary>
        protected void btnPrev_Click(object sender, DirectEventArgs e)
        {
            int pageIndex = Convert.ToInt32(txtPageIndex.Text);
            if (pageIndex > 0)
            {
                pageIndex--;
                txtPageIndex.Text = pageIndex.ToString();
                GirdPanelBind("meeting", "*", "id", "asc", 10, pageIndex, "");
            }
        }

        /// <summary>
        /// 跳转到尾页
        /// </summary>
        protected void btnLast_Click(object sender, DirectEventArgs e)
        {
            int pageIndex = GetPangCount() / 10 + 1;
            txtPageIndex.Text = pageIndex.ToString();
            GirdPanelBind("meeting", "*", "id", "asc", 10, pageIndex, "");
        }

        /// <summary>
        /// 跳转到指定页面
        /// </summary>
        protected void btnGopage_Click(object sender, DirectEventArgs e)
        {
            int pageIndex = Convert.ToInt32(txtPageIndex.Text);
            txtPageIndex.Text = txtPageIndex.Text;
            GirdPanelBind("meeting", "*", "id", "asc", 10, pageIndex, "");
        }
        #endregion

    }
}

表结构:

效果图:

posted @ 2012-12-28 15:45  春天又来了  阅读(589)  评论(0编辑  收藏  举报