.net自制实用的分页控件

  哈哈,第一次自己写通用自定义控件,发上来请大家多多指教。经本人多次测试,绝对可以使用哦!!

 

作用:分页控制

基本模样:上一页 1... 2 3 4 5 6 ...9 下一页

功能点:支持自定义样式,支持并发处理,支持设置页码控件数

 

注意点:页面不能禁用ViewState

 

使用说明:

1.控件配置参数(可任意修改)下的变量值均可修改,并且可以在web.config文件的<appSettings>下配置与参数相应的的值。

2.其他变量值不可修改。

3.有三个事件的处理程序必须由父页面进行定义,由分页控件进行调用:

BindPageData 用于绑定相应数据到父页面;

 

SaveNextPageToViewState 用于在父页面记录当前页码,父页面要创建一个ViewState来保存nextPage的内容,在非初次生成控件时作为参数传给InitPaging;(父页面调用InitPaging时,格式:InitPaging((ViewState["nextPage"] == null ? null : Convert.ToString(ViewState["nextPage"])), dt.Rows.Count);其中ViewState["nextPage"]是在SaveNextPageToViewState的处理程序中新建并赋值的。)

注意点:该处理程序要在调用InitPaging方法前就绑定。

 

WhenComplicated 用于当出现并发问题时(如数据不同步时)作出处理,在父页面定制处理程序(该事件与IS_MANAGE_COMPLICATION变量一起设置,IS_MANAGE_COMPLICATION为true时,才会调用WhenComplicated的处理程序处理并发情况,否则不处理。)

注意点:该处理程序要在调用InitPaging方法前就绑定。

 

下面是代码哦!

 

 

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public delegate void PageControler(int nextPage,int perPageRecordCount);

public delegate void SaveNextPage(string nextPage);

public delegate void ManageComplication();

 

public partial class NewPageControl : System.Web.UI.UserControl

{

    /*-------控件配置参数(可任意修改)---------*/

    public readonly int PER_PAGE_RECORD_COUNT = 1;//每页的记录数

    private readonly int NUMBER_BUTTON_COUNT = 3;//数字控件数量

    private readonly int REFERENCE_PAGE_INDEX = 2;//数字控件移动的初始参考系数(以该序号的控件为界,决定数字控件是否移动)

 

    private readonly string UNSELECTED_CSS_CLASS = "";//分页码控件——未选中的样式(不用时设为空字符串)

    private readonly string SELECTED_CSS_CLASS = "";//分页码控件——选中的样式(不用时设为空字符串)

    private readonly string UNSELECTED_INNER_STYLE = "padding:0 5px 0 5px;color:#515151;";//分页码控件——内嵌未选中的样式(不用时设为空字符串)

    private readonly string SELECTED_INNER_STYLE = "padding:0 5px 0 5px;";//分页码控件——内嵌选中的样式(不用时设为空字符串)

    private readonly bool SET_ENABLE = true;//标识是否设置控件的enable属性

 

    private readonly string FIRST_STR = "1...";//首页跳转控件文字

    private string lastStr = String.Empty;//最后一页跳转控件文字,在InitPaging中赋值

    private readonly string PREVIOUS_COMMAND_STR = "back";

    private readonly string NEXT_COMMAND_STR = "next";

    private readonly string PREVIOUS_TEXT_STR = "上一页";

    private readonly string NEXT_TEXT_STR = "下一页";

 

    private readonly bool IS_MANAGE_COMPLICATION = true;//是否处理并发情况,true:处理;false:不处理

 

    /*------ViewState名称(一般不用修改)--------*/

    private readonly string UPDATE_OR_NOT = "UpdateOrNot";//用于决定ViewState中的realNextPage和StanderPageIndex是否更新为最新状态,true:更新,false:不更新

    private readonly string CURRENT_REFERENCE_PAGE_INDEX = "currentReferencePageIndex";//用于记录现在的数字控件移动的参考系数

    private readonly string REAL_NEXT_PAGE_INDEX = "realNextPageIndex";//用于记录现在要显示的页码

    private readonly string TOTAL_RECORD_COUNT = "totalRecordCount";//用于记录记录总数

 

    /*-------普通成员变量---------*/

    private int pageCount = 0;

 

    /*-------由父画面定义实现代码的事件对象(父页面必须实现)-------*/

    public event PageControler BindPageData = null;//根据分页码绑定列表数据

    public event SaveNextPage SaveNextPageToViewState = null;//记录当前分页码,父页面要创建一个ViewState来保存nextPage的内容,在非初次生成控件时作为参数传给InitPaging

    public event ManageComplication WhenComplicated = null;//当出现并发问题时作出处理,在父页面定制处理方式

 

    public NewPageControl()

    {

        //从web.config读取控件配置参数

        PER_PAGE_RECORD_COUNT = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["PER_PAGE_RECORD_COUNT"]) ? PER_PAGE_RECORD_COUNT : Convert.ToInt32(ConfigurationManager.AppSettings["PER_PAGE_RECORD_COUNT"]));

        NUMBER_BUTTON_COUNT = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["NUMBER_BUTTON_COUNT"]) ? NUMBER_BUTTON_COUNT : Convert.ToInt32(ConfigurationManager.AppSettings["NUMBER_BUTTON_COUNT"]));

        REFERENCE_PAGE_INDEX = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["REFERENCE_PAGE_INDEX"]) ? REFERENCE_PAGE_INDEX : Convert.ToInt32(ConfigurationManager.AppSettings["REFERENCE_PAGE_INDEX"]));

 

        UNSELECTED_CSS_CLASS = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["UNSELECTED_CSS_CLASS"]) ? UNSELECTED_CSS_CLASS :ConfigurationManager.AppSettings["UNSELECTED_CSS_CLASS"]);

        SELECTED_CSS_CLASS = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["SELECTED_CSS_CLASS"]) ? SELECTED_CSS_CLASS : ConfigurationManager.AppSettings["SELECTED_CSS_CLASS"]);

        UNSELECTED_INNER_STYLE = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["UNSELECTED_INNER_STYLE"]) ? UNSELECTED_INNER_STYLE : ConfigurationManager.AppSettings["UNSELECTED_INNER_STYLE"]);

        SELECTED_INNER_STYLE = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["SELECTED_INNER_STYLE"]) ? SELECTED_INNER_STYLE : ConfigurationManager.AppSettings["SELECTED_INNER_STYLE"]);

        SET_ENABLE = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["SET_ENABLE"]) ? SET_ENABLE : Convert.ToBoolean(ConfigurationManager.AppSettings["SET_ENABLE"]));

 

        PREVIOUS_COMMAND_STR = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["PREVIOUS_COMMAND_STR"]) ? PREVIOUS_COMMAND_STR : ConfigurationManager.AppSettings["PREVIOUS_COMMAND_STR"]);

        NEXT_COMMAND_STR = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["NEXT_COMMAND_STR"]) ? NEXT_COMMAND_STR : ConfigurationManager.AppSettings["NEXT_COMMAND_STR"]);

        PREVIOUS_TEXT_STR = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["PREVIOUS_TEXT_STR"]) ? PREVIOUS_TEXT_STR : ConfigurationManager.AppSettings["PREVIOUS_TEXT_STR"]);

        NEXT_TEXT_STR = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["NEXT_TEXT_STR"]) ? NEXT_TEXT_STR : ConfigurationManager.AppSettings["NEXT_TEXT_STR"]);

 

    }

 

    /// <summary>

    /// 生成分页控件

    /// 注意:

    /// 初次生成分页控件时——nextPage为null;

    /// 非初次生成分页控件时——nextPage为 要显示的页码 或 控件的CommandName值

    /// </summary>

    /// <param name="nextPage">为要跳转到的页码</param>

    /// <param name="totalRecordCount">显示的记录总数</param>

    public void InitPaging(string nextPage,int totalRecordCount)

    {

        if(totalRecordCount == 0)

        {

            return ;

        }

 

        if (ViewState[UPDATE_OR_NOT] == null || Convert.ToBoolean(ViewState[UPDATE_OR_NOT]))

        {

            ViewState[UPDATE_OR_NOT] = false;

        }

        else if (!Convert.ToBoolean(ViewState[UPDATE_OR_NOT]))

        {

            ViewState[UPDATE_OR_NOT] = true;

        }

 

        //并发处理

        if (IS_MANAGE_COMPLICATION && Convert.ToBoolean(ViewState[UPDATE_OR_NOT]) && Convert.ToInt32(ViewState[TOTAL_RECORD_COUNT]) != totalRecordCount)

        {

            if (this.WhenComplicated != null)

            {

                WhenComplicated();

            }

        }

 

        ViewState[TOTAL_RECORD_COUNT] = totalRecordCount;

 

        this.pageCount = totalRecordCount / this.PER_PAGE_RECORD_COUNT + (totalRecordCount % this.PER_PAGE_RECORD_COUNT == 0 ? 0 : 1);

 

        this.lastStr = "..." + this.pageCount;//最后一页跳转控件文字

 

        LinkButton back = new LinkButton();

        back.CommandName = PREVIOUS_COMMAND_STR;

        back.Text = PREVIOUS_TEXT_STR;

        back.Command += new CommandEventHandler(PageChange);

 

        this.Controls.Add(back);

 

        //初始化时

        if (nextPage == null)

        {

            SetCssTool(back, false, this.SET_ENABLE);

 

            //生成数字控件

            LinkButton num1 = new LinkButton();

            num1.CommandName = "1";

            num1.Text = "1";

            num1.Command += PageChange;

            SetCssTool(num1, false, this.SET_ENABLE);

            this.Controls.Add(num1);

 

            int realNumberButtonCount = (this.pageCount >= this.NUMBER_BUTTON_COUNT ? this.NUMBER_BUTTON_COUNT : this.pageCount);

            for (int index = 2; index <= realNumberButtonCount; ++index)

            {

                LinkButton num = new LinkButton();

                num.CommandName = Convert.ToString(index);

                num.Text = Convert.ToString(index);

                num.Command += PageChange;

                SetCssTool(num, true, this.SET_ENABLE);

                this.Controls.Add(num);

            }

            if (this.pageCount > this.NUMBER_BUTTON_COUNT)

            {

                LinkButton lastPage = new LinkButton();

                lastPage.CommandName = this.lastStr;

                lastPage.Text = this.lastStr;

                lastPage.Command += PageChange;

                SetCssTool(lastPage, true, this.SET_ENABLE);

                this.Controls.Add(lastPage);

            }

 

            LinkButton next = new LinkButton();

            next.CommandName = NEXT_COMMAND_STR;

            next.Text = NEXT_TEXT_STR;

            next.Command += PageChange;

            SetCssTool(next, (this.pageCount == 1 ? false : true), this.SET_ENABLE);

            this.Controls.Add(next);

 

            ViewState[this.CURRENT_REFERENCE_PAGE_INDEX] = this.REFERENCE_PAGE_INDEX;

            ViewState[this.REAL_NEXT_PAGE_INDEX] = 1;

        }

        else

        {

            //非初始化时

            int realNextPage = 0;

            if (nextPage.Equals(this.FIRST_STR))

            {

                realNextPage = 1;

            }

            else if (nextPage.Equals(this.lastStr))

            {

                realNextPage = this.pageCount;

            }

            else if (nextPage.Equals(NEXT_COMMAND_STR))

            {

                realNextPage = Convert.ToInt32(ViewState[this.REAL_NEXT_PAGE_INDEX]) + 1;

            }

            else if (nextPage.Equals(PREVIOUS_COMMAND_STR))

            {

                realNextPage = Convert.ToInt32(ViewState[this.REAL_NEXT_PAGE_INDEX]) - 1;

            }

            else

            {

                realNextPage = Convert.ToInt32(nextPage);

            }

 

            if (Convert.ToBoolean(ViewState[UPDATE_OR_NOT]))

            {

                ViewState[this.REAL_NEXT_PAGE_INDEX] = realNextPage;

            }

 

            //总页数小于等于数字控件数时

            if (this.pageCount <= this.NUMBER_BUTTON_COUNT)

            {

                SetCssTool(back, (realNextPage == 1 ? false : true), this.SET_ENABLE);

 

                for (int index = 1; index <= this.pageCount; ++index)

                {

                    LinkButton num = new LinkButton();

                    num.CommandName = Convert.ToString(index);

                    num.Command += new CommandEventHandler(PageChange);

                    num.Text = Convert.ToString(index);

                    SetCssTool(num, (realNextPage == index ? false : true), this.SET_ENABLE);

                    this.Controls.Add(num);

                }

 

                LinkButton next = new LinkButton();

                next.CommandName = NEXT_COMMAND_STR;

                next.Text = NEXT_TEXT_STR;

                next.Command += new CommandEventHandler(PageChange);

                SetCssTool(next, (this.pageCount == realNextPage ? false : true), this.SET_ENABLE);

                this.Controls.Add(next);

            }

            else

            {

            //总分页数大于数字控件数时

 

                // 点击1...

                if(nextPage.Equals(this.FIRST_STR))

                {

                    SetCssTool(back, false, this.SET_ENABLE);

 

                    for (int index = 1; index <= this.NUMBER_BUTTON_COUNT; ++index)

                    {

                        LinkButton num = new LinkButton();

                        num.Text = Convert.ToString(index);

                        num.CommandName = Convert.ToString(index);

                        num.Command += new CommandEventHandler(PageChange);

                        SetCssTool(num, (index == realNextPage ? false : true), this.SET_ENABLE);

                        this.Controls.Add(num);

                    }

 

                    LinkButton last = new LinkButton();

                    last.Text = this.lastStr;

                    last.Command += new CommandEventHandler(PageChange);

                    last.CommandName = this.lastStr;

                    SetCssTool(last, true, this.SET_ENABLE);

                    this.Controls.Add(last);

 

                    LinkButton next = new LinkButton();

                    next.CommandName = NEXT_COMMAND_STR;

                    next.Text = NEXT_TEXT_STR;

                    next.Command += new CommandEventHandler(PageChange);

                    SetCssTool(next, true, this.SET_ENABLE);

                    this.Controls.Add(next);

 

                    ViewState[this.CURRENT_REFERENCE_PAGE_INDEX] = this.REFERENCE_PAGE_INDEX;

 

                }

 

                // 点击...最后页码

                else if (nextPage.Equals(this.lastStr))

                {

 

                    SetCssTool(back, true, this.SET_ENABLE);

 

                    LinkButton first = new LinkButton();

                    first.Text = this.FIRST_STR;

                    first.Command += new CommandEventHandler(PageChange);

                    first.CommandName = this.FIRST_STR;

                    SetCssTool(first, true, this.SET_ENABLE);

                    this.Controls.Add(first);

                    int numStr = 0;

                    for (int index = this.NUMBER_BUTTON_COUNT-1; index >= 0; --index )

                    {

                        numStr = this.pageCount - index;

                        LinkButton num = new LinkButton();

                        num.Text = Convert.ToString(numStr);

                        num.CommandName = Convert.ToString(numStr);

                        num.Command += new CommandEventHandler(PageChange);

                        SetCssTool(num, (numStr == realNextPage ? false : true), this.SET_ENABLE);

                        this.Controls.Add(num);      

                    }

 

                    LinkButton next = new LinkButton();

                    next.Text = NEXT_TEXT_STR;

                    next.Command += new CommandEventHandler(PageChange);

                    next.CommandName = NEXT_COMMAND_STR;

                    SetCssTool(next, true, this.SET_ENABLE);

                    this.Controls.Add(next);

 

                    ViewState[this.CURRENT_REFERENCE_PAGE_INDEX] = totalRecordCount - this.NUMBER_BUTTON_COUNT + this.REFERENCE_PAGE_INDEX ;

                }

                // 数字向左移动

                else if (Convert.ToInt32(ViewState[this.CURRENT_REFERENCE_PAGE_INDEX]) < realNextPage)

                {

                    SetCssTool(back, true, this.SET_ENABLE);

 

                    LinkButton first = new LinkButton();

                    first.Text = this.FIRST_STR;

                    first.Command += new CommandEventHandler(PageChange);

                    first.CommandName = this.FIRST_STR;

                    SetCssTool(first, true, this.SET_ENABLE);

                    this.Controls.Add(first);

 

                    // 不用移动时

                    if (this.pageCount - realNextPage <this.NUMBER_BUTTON_COUNT - this.REFERENCE_PAGE_INDEX)

                    {

                        int numStr = 0;

                        for (int index = this.NUMBER_BUTTON_COUNT-1; index >=0; --index)

                        {

                            numStr = this.pageCount - index;

                            LinkButton num = new LinkButton();

                            num.Text = Convert.ToString(numStr);

                            num.CommandName = Convert.ToString(numStr);

                            num.Command += new CommandEventHandler(PageChange);

                            SetCssTool(num, (numStr == realNextPage ? false : true), this.SET_ENABLE);

                            this.Controls.Add(num);                            

                        }

 

                        LinkButton next = new LinkButton();

                        next.CommandName = NEXT_COMMAND_STR;

                        next.Text = NEXT_TEXT_STR;

                        next.Command += new CommandEventHandler(PageChange);

                        SetCssTool(next, (this.pageCount == realNextPage ? false : true), this.SET_ENABLE);

                        this.Controls.Add(next);

                    }

 

                    //要移动时

                    else

                    {

                        // 分页数足够左移

                        if (this.pageCount - realNextPage >= this.NUMBER_BUTTON_COUNT - this.REFERENCE_PAGE_INDEX)

                        {

                            int numStr = 0;

                            for (int index = 1 ; index <= this.NUMBER_BUTTON_COUNT; ++index)

                            {

                                numStr = realNextPage - this.REFERENCE_PAGE_INDEX + index;

                                LinkButton num = new LinkButton();

                                num.Text = Convert.ToString(numStr);

                                num.CommandName = Convert.ToString(numStr);

                                num.Command += new CommandEventHandler(PageChange);

                                SetCssTool(num, (numStr == realNextPage ? false : true), this.SET_ENABLE);

                                this.Controls.Add(num);

                            }

 

                            if (this.pageCount - realNextPage != this.NUMBER_BUTTON_COUNT - this.REFERENCE_PAGE_INDEX)

                            {

                                LinkButton last = new LinkButton();

                                last.Text = this.lastStr;

                                last.Command += new CommandEventHandler(PageChange);

                                last.CommandName = this.lastStr;

                                SetCssTool(last, true, this.SET_ENABLE);

                                this.Controls.Add(last);

                            }

 

                            if (Convert.ToBoolean(ViewState[UPDATE_OR_NOT]))

                            {

                                ViewState[this.CURRENT_REFERENCE_PAGE_INDEX] = realNextPage;

                            }

                        }

 

                        // 不足够左移

                        else

                        {

                            int innerIndex = 1;

                            for (int index = this.pageCount-this.NUMBER_BUTTON_COUNT + 1; index <= this.pageCount; ++index)

                            {

                                LinkButton num = new LinkButton();

                                num.Text = Convert.ToString(index);

                                num.CommandName = Convert.ToString(index);

                                num.Command += new CommandEventHandler(PageChange);

                                SetCssTool(num, (index == realNextPage ? false : true), this.SET_ENABLE);

                                this.Controls.Add(num);

 

                                if (innerIndex == this.REFERENCE_PAGE_INDEX && Convert.ToBoolean(ViewState[UPDATE_OR_NOT]))

                                {

                                    ViewState[this.CURRENT_REFERENCE_PAGE_INDEX] = index;

                                }

                                ++innerIndex;

                            }

                        }

 

                        LinkButton next = new LinkButton();

                        next.CommandName = NEXT_COMMAND_STR;

                        next.Text = NEXT_TEXT_STR;

                        next.Command += new CommandEventHandler(PageChange);

                        SetCssTool(next, (this.pageCount == realNextPage ? false : true), this.SET_ENABLE);

                        this.Controls.Add(next);

 

                    }

 

                }

 

                // 点击参考系数字控件时,不移动时

                else if (Convert.ToInt32(ViewState[this.CURRENT_REFERENCE_PAGE_INDEX])==realNextPage)

                {

                    SetCssTool(back, true, this.SET_ENABLE);

 

                    // 靠近最后一页那边

                    if (Convert.ToInt32(ViewState[this.CURRENT_REFERENCE_PAGE_INDEX]) != this.REFERENCE_PAGE_INDEX)

                    {

                        LinkButton first = new LinkButton();

                        first.Text = this.FIRST_STR;

                        first.Command += new CommandEventHandler(PageChange);

                        first.CommandName = this.FIRST_STR;

                        SetCssTool(first, true, this.SET_ENABLE);

                        this.Controls.Add(first);

                    }

 

                    int numStr = 0;

                    for (int index = 1; index <= this.NUMBER_BUTTON_COUNT; ++index)

                    {

                        numStr = Convert.ToInt32(ViewState[this.CURRENT_REFERENCE_PAGE_INDEX]) - this.REFERENCE_PAGE_INDEX + index;

                        LinkButton num = new LinkButton();

                        num.Text = Convert.ToString(numStr);

                        num.CommandName = Convert.ToString(numStr);

                        num.Command += new CommandEventHandler(PageChange);

                        SetCssTool(num, (numStr == realNextPage ? false : true), this.SET_ENABLE);

                        this.Controls.Add(num);

                    }

 

                    // 靠近第一页那边

                    if (Convert.ToInt32(ViewState[this.CURRENT_REFERENCE_PAGE_INDEX]) == this.REFERENCE_PAGE_INDEX)

                    {

                        LinkButton last = new LinkButton();

                        last.Text = this.lastStr;

                        last.Command += new CommandEventHandler(PageChange);

                        last.CommandName = this.lastStr;

                        SetCssTool(last, true, this.SET_ENABLE);

                        this.Controls.Add(last);

                    }

 

                    LinkButton next = new LinkButton();

                    next.CommandName = NEXT_COMMAND_STR;

                    next.Text = NEXT_TEXT_STR;

                    next.Command += new CommandEventHandler(PageChange);

                    SetCssTool(next, true, this.SET_ENABLE);

                    this.Controls.Add(next);

 

                }

 

 

                // 数字向右移动

                else if (Convert.ToInt32(ViewState[this.CURRENT_REFERENCE_PAGE_INDEX]) > realNextPage)

                {

                    SetCssTool(back, (realNextPage == 1 ? false : true), this.SET_ENABLE);

 

                    // 不用移动时

                    if (Convert.ToInt32(ViewState[this.CURRENT_REFERENCE_PAGE_INDEX]) == this.REFERENCE_PAGE_INDEX)

                    {

                        for (int index = 1; index <= this.NUMBER_BUTTON_COUNT; ++index)

                        {

                            LinkButton num = new LinkButton();

                            num.Text = Convert.ToString(index);

                            num.CommandName = Convert.ToString(index);

                            num.Command += new CommandEventHandler(PageChange);

                            SetCssTool(num, (index == realNextPage ? false : true), this.SET_ENABLE);

                            this.Controls.Add(num);

                        }

                    }

 

                    // 要移动时

                    else

                    {

                        // 分页数足够右移

                        if (realNextPage-this.REFERENCE_PAGE_INDEX >= 0)

                        {

                            if (realNextPage - this.REFERENCE_PAGE_INDEX != 0)

                            {

                                LinkButton first = new LinkButton();

                                first.Text = this.FIRST_STR;

                                first.Command += new CommandEventHandler(PageChange);

                                first.CommandName = this.FIRST_STR;

                                SetCssTool(first, true, this.SET_ENABLE);

                                this.Controls.Add(first);

                            }

                            int pageNumber = 0;

                            for (int index = 1; index <= this.NUMBER_BUTTON_COUNT; ++index)

                            {

                                pageNumber = realNextPage - this.REFERENCE_PAGE_INDEX + index;

                                LinkButton num = new LinkButton();

                                num.Text = Convert.ToString(pageNumber);

                                num.CommandName = Convert.ToString(pageNumber);

                                num.Command += new CommandEventHandler(PageChange);

                                SetCssTool(num, (pageNumber == realNextPage ? false : true), this.SET_ENABLE);

                                this.Controls.Add(num);

                            }

 

                            if (Convert.ToBoolean(ViewState[UPDATE_OR_NOT]))

                            {

                                ViewState[this.CURRENT_REFERENCE_PAGE_INDEX] = realNextPage;

                            }

                        }

 

                        // 不足够右移

                        else

                        {

                            for (int index = 1; index <= this.NUMBER_BUTTON_COUNT; ++index)

                            {

                                LinkButton num = new LinkButton();

                                num.Text = Convert.ToString(index);

                                num.CommandName = Convert.ToString(index);

                                num.Command += new CommandEventHandler(PageChange);

                                SetCssTool(num, (index == realNextPage ? false : true), this.SET_ENABLE);

                                this.Controls.Add(num);

                            }

 

                            if (Convert.ToBoolean(ViewState[UPDATE_OR_NOT]))

                            {

                                ViewState[this.CURRENT_REFERENCE_PAGE_INDEX] = this.REFERENCE_PAGE_INDEX;

                            }

                        }

 

                    }

 

 

                    LinkButton last = new LinkButton();

                    last.Text = this.lastStr;

                    last.Command += new CommandEventHandler(PageChange);

                    last.CommandName = this.lastStr;

                    SetCssTool(last, true, this.SET_ENABLE);

                    this.Controls.Add(last);

 

                    LinkButton next = new LinkButton();

                    next.CommandName = NEXT_COMMAND_STR;

                    next.Text = NEXT_TEXT_STR;

                    next.Command += new CommandEventHandler(PageChange);

                    SetCssTool(next, true, this.SET_ENABLE);

                    this.Controls.Add(next);

                }

 

            }

        }

        if (SaveNextPageToViewState != null)

        {

            this.SaveNextPageToViewState(nextPage);

        }

    }

 

    /// <summary>

    /// 点击控件时,重新生成分页控件并绑定相应的数据到父页面上

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void PageChange(object sender, EventArgs e)

    {

        ClearAllControls();

        string commandName = ((LinkButton)sender).CommandName;

        InitPaging(commandName, Convert.ToInt32(ViewState[TOTAL_RECORD_COUNT]));

 

        if (this.BindPageData != null)

        {

            int currentPage = 0;

            if (commandName.Equals(this.FIRST_STR))

            {

                currentPage = 1;

            }

            else if (commandName.Equals(this.lastStr))

            {

                currentPage = this.pageCount;

            }

            else if (commandName.Equals(this.NEXT_COMMAND_STR))

            {

                currentPage = Convert.ToInt32(ViewState[this.REAL_NEXT_PAGE_INDEX]) + 1;

            }

            else if (commandName.Equals(this.PREVIOUS_COMMAND_STR))

            {

                currentPage = Convert.ToInt32(ViewState[this.REAL_NEXT_PAGE_INDEX]) - 1;

            }

            else

            {

                currentPage = Convert.ToInt32(commandName);

            }

            this.BindPageData(currentPage, this.PER_PAGE_RECORD_COUNT);

        }

    }

 

    /// <summary>

    /// 清理所有分页控件

    /// </summary>

    public void ClearAllControls()

    {

        this.Controls.Clear();

    }

 

    /// <summary>

    /// 设置控件样式

    /// </summary>

    /// <param name="lbtn"></param>

    private void SetCssTool(LinkButton lbtn,bool enable,bool setEnable)

    {

        if (enable)

        {

            if (setEnable)

            {

                lbtn.Enabled = true;

            }

 

            if (!String.IsNullOrEmpty(this.UNSELECTED_CSS_CLASS))

            {

                lbtn.CssClass = this.UNSELECTED_CSS_CLASS;

            }

 

            if (!String.IsNullOrEmpty(this.UNSELECTED_INNER_STYLE))

            {

                lbtn.Attributes["style"] = this.UNSELECTED_INNER_STYLE;

            }

        }

        else

        {

            if (setEnable)

            {

                lbtn.Enabled = false;

            }

 

            if (!String.IsNullOrEmpty(this.SELECTED_CSS_CLASS))

            {

                lbtn.CssClass = this.SELECTED_CSS_CLASS;

            }

 

            if (!String.IsNullOrEmpty(this.SELECTED_INNER_STYLE))

            {

                lbtn.Attributes["style"] = this.SELECTED_INNER_STYLE;

            }

        }

    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值