自定义分页排序

这个博客介绍了如何在ASP.NET应用程序中实现自定义分页和排序功能。首先,通过`Page_Load`事件处理程序设置分页参数并计算记录总数。接着,提供了几个辅助方法用于计算额外页数、模数页数和获取记录计数。然后,根据用户是否进行排序,构造不同的SQL查询以获取分页数据。博客还展示了如何响应用户对表格的排序操作,并提供了删除记录的功能。
摘要由CSDN通过智能技术生成

{
    Int32 PageSize = 0;
    Int32 PageCount, RecCount, CurrentPage, Pages;
    bool isSorting = false;  //True if the user is sorting a column
    int sortColumn;    //The column number the user is trying to sort
    int sortBand;
    string sortSequence = " ASC ";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            PageSize = this.uwgKeyWord.DisplayLayout.Pager.PageSize;

            RecCount = Calc();

            //Record Page count.
            PageCount = RecCount / PageSize + OverPage();

            //ViewState["PageCounts"] = RecCount / PageSize - ModPage();
            ViewState["PageCounts"] = RecCount / PageSize;

            ViewState["PageIndex"] = 0;
            ViewState["JumpPages"] = PageCount;
            ViewState["Order"] = " ORDER BY ID ASC ";

            //TDataBind();
            InitData();
            this.CheckUserType();
        }
    }

    public Int32 OverPage()
    {
        PageSize = this.uwgKeyWord.DisplayLayout.Pager.PageSize;

        Int32 pages = 0;
        if (RecCount % PageSize != 0)
            pages = 1;
        else
            pages = 0;
        return pages;
    }

    public Int32 ModPage()
    {
        PageSize = this.uwgKeyWord.DisplayLayout.Pager.PageSize;

        Int32 pages = 0;
        if (RecCount % PageSize == 0 && RecCount != 0)
            pages = 1;
        else
            pages = 0;
        return pages;
    }

    public static Int32 Calc()
    {
        Int32 RecordCount = 0;
        SqlCommand MyCmd = new SqlCommand("select count(*) as co from failurelog ", MyCon());
        SqlDataReader dr = MyCmd.ExecuteReader();
        if (dr.Read())
            RecordCount = Int32.Parse(dr["co"].ToString());
        MyCmd.Connection.Close();
        return RecordCount;
    }

    public static Int32 Calc(string sqlSearch)
    {
        Int32 RecordCount = 0;
        SqlCommand MyCmd = new SqlCommand(sqlSearch, MyCon());
        SqlDataReader dr = MyCmd.ExecuteReader();
        if (dr.Read())
            RecordCount = Int32.Parse(dr["co"].ToString());
        MyCmd.Connection.Close();
        return RecordCount;
    }

    public static SqlConnection MyCon()
    {
        SqlConnection MyConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
        MyConnection.Open();
        return MyConnection;
    }

    #region Bind UltraWebGrid
    /// <summary>
    /// Bind data to UltraWebGrid.
    /// </summary>
    private void BindData()
    {
        string querySql = "";
        string sqlSearch = "";

        PageSize = this.uwgKeyWord.DisplayLayout.Pager.PageSize;

        if (!string.IsNullOrEmpty(QueryCondition()))
        {
            sqlSearch = "select Count(*) as co from failurelog " + QueryCondition();
            RecCount = Calc(sqlSearch);
            //Record Page count.
            PageCount = RecCount / PageSize + OverPage();
        }
        else
        {
            RecCount = Calc();
        }

        ViewState["PageCounts"] = RecCount / PageSize;

        CurrentPage = (int)ViewState["PageIndex"];
        Pages = (int)ViewState["PageCounts"];

        if (Pages == 0)
        {
            Pages = 1;
        }

        if (PageCount == 0)
        {
            PageCount = 1;
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值