天天看点

asp.net RadGrid分页功能扩展Demo

1、在权限判断代码后面增加如下代码

     VSDataTable1 = new DataSet();

    public void BindRadGrid()

    {

             //...

             this.RadGrid1.DataSource = ds;

            this.RadGrid1.DataBind();

            ViewState["ajbb"] = ds;

    }

    //RadGrid分页功能扩展代码

    protected void RadGrid1_PageIndexChanged(object source, Telerik.WebControls.GridPageChangedEventArgs e)

    {

        //分页代码

        this.RadGrid1.DataSource = VSDataTable1;

        this.RadGrid1.CurrentPageIndex = e.NewPageIndex;

        this.RadGrid1.DataBind();

    }

    #region 分页功能扩展 2012-06-26

    public void btnHidden_Click(object sender, EventArgs e)

    {

        int currentPageIndex = int.Parse(hf.Value);

        this.RadGrid1.DataSource = VSDataTable1;

        this.RadGrid1.CurrentPageIndex = currentPageIndex-1;

        this.RadGrid1.DataBind();

    }

    #endregion

   #region 分页通用代码

   /// <summary>

    /// 存放在ViewState的DataTable

    /// </summary>

    private DataSet VSDataTable1

        get { return ViewState["ajbb"] as DataSet; }

        set { ViewState["ajbb"] = value; }

    /// <summary>

    /// 普通的绑定资料

    private void PageDataBind()

    /// 绑定资料加换页

    /// <param name="getPageIndex">新页面Index</param>

    private void PageDataBind(Int32 getPageIndex)

        this.RadGrid1.CurrentPageIndex = getPageIndex;

   #endregion

2、前台页面中

将RadGrid中的<PagerStyle/>替换成"></PagerStyle>

<PagerStyle Mode="NextPrevAndNumeric" NextPagesToolTip="下一页" NextPageText="下一页" NextPageToolTip="下一页"

                                            PagerTextFormat="分页 : {4}  <input type='text' name='txtNum' id='txtNum' value='1' style='width:20px'/><input type='button' id='btnConfirm' value='确定'  onclick='splitPageByHand();'/>"

                                            PrevPagesToolTip="上一页" PrevPageText="上一页" PrevPageToolTip="上一页"></PagerStyle>

3、在</rad:Grid>后面增加2个控件,代码如下:

<asp:HiddenField ID="hf" runat="server" />

<asp:Button ID="btnHidden" runat="server" Style="display: none;" OnClick="btnHidden_Click" />

4、在</html>标记后面添加如下js代码

<!--通用手动分页代码-->

<script type="text/javascript">

    //手动填写页码进行分页跳转

    function splitPageByHand() {

        if (!isNaN(document.getElementById("txtNum").value)) {

            if (document.getElementById("txtNum").value > parseInt('<%=RadGrid1.PageCount%>') || document.getElementById("txtNum").value <= 0) {

                alert('输入数字不在范围内!');

                document.getElementById("txtNum").value = "1";

            }

            else {

                document.getElementById("hf").value = document.getElementById("txtNum").value;

                document.getElementById("btnHidden").click();

        }

        else {

            alert('请输入数字!');

            document.getElementById("txtNum").value = "1";

    //为input控件重新赋值

    if(document.getElementById("txtNum")!=null){

      document.getElementById("txtNum").value = '<%=RadGrid1.CurrentPageIndex+1 %>';

</script>

以下是代码

继续阅读