首页蓝耳朵|小小蓝耳朵广州图书馆外文室英语儿童读物资源介绍网佛教青年之友旧版收集永硕E盘Phonics Short Vowels Game 
给GridView的自带分页功能加上分页信息和页码切换文本框
所属栏目:ASP.NET(webform)  时间:2009-05-05 20:58  作者:狼子

例子本网站的列表页面,如下图:

 

方法:给GridView的BottomPagerRow加前后两个单元格,第一个单元格用于显示记录数和分页信息,最后一个单元格用于手动录入页码切换

#region 给GridView的分页加上数据信息
    /// <summary>
    /// 给GridView的分页加上数据信息
    /// </summary>
    /// <param name="rowsCount">数据总数</param>
    /// <param name="gv">GridView控件</param>
    public static void ReWriteGridViewPaging(int rowsCount, GridView gv)
    {
        GridViewRow gvrp = gv.BottomPagerRow;
        if (gvrp != null)
        {
            Literal lt = new Literal();
            lt.Text = "共有记录:" + rowsCount.ToString() + " 每页" + gv.PageSize.ToString() + "条 " + (gv.PageIndex + 1).ToString() + "/" + gv.PageCount.ToString() + "页";

            Literal ltInput = new Literal();

            #region 使用文本录入框转换页
            System.Text.StringBuilder linkText = new System.Text.StringBuilder("");
            linkText.Append(" 转到 <input type=\"text\" id=\"myListPage\" name=\"myListPage\" style=\"width:40px;\" /> 页 <input type=\"button\" value=\"确定\" class=\"button2\" onclick=\"listChangePage();\" />");
            linkText.AppendLine("<script type=\"text/javascript\">");
            linkText.AppendLine("function TestRgexp(s){ var re = /^[0-9]*[1-9][0-9]*$/; return re.test(s); }");
            linkText.AppendLine("function listChangePage()");
            linkText.AppendLine("{");
            linkText.AppendLine("    var pageObj, pageValue, myMaxPage, myNowPage;");
            linkText.AppendLine("    myMaxPage = " + gv.PageCount.ToString() + ";");
            linkText.AppendLine("    myNowPage = " + Convert.ToString(gv.PageIndex + 1) + ";");
            linkText.AppendLine("    pageObj = document.getElementById(\"myListPage\");");
            linkText.AppendLine("    if(pageObj)");
            linkText.AppendLine("    {");
            linkText.AppendLine("        pageValue = pageObj.value;");
            linkText.AppendLine("        if ( pageValue == myNowPage ) return;");
            linkText.AppendLine("        if ( TestRgexp(pageValue) )");
            linkText.AppendLine("        {");
            linkText.AppendLine("            if(pageValue - myMaxPage > 0)");
            linkText.AppendLine("            {");
            linkText.AppendLine("               if(myNowPage == myMaxPage)");
            linkText.AppendLine("               {");
            linkText.AppendLine("                  pageValue = 1;");
            linkText.AppendLine("               }");
            linkText.AppendLine("               else");
            linkText.AppendLine("               {");
            linkText.AppendLine("                  pageValue = myMaxPage;");
            linkText.AppendLine("               }");
            linkText.AppendLine("            }");
            linkText.AppendLine("            pageValue = \"Page$\" + pageValue;");
            linkText.AppendLine("            javascript:__doPostBack('" + gv.ClientID.Replace("_", "$") + "',pageValue);");
            linkText.AppendLine("        }");
            linkText.AppendLine("        else");
            linkText.AppendLine("        {");
            linkText.AppendLine("            alert(\"页码必须输入正整数\");");
            linkText.AppendLine("        }");
            linkText.AppendLine("    }");
            linkText.AppendLine("}");
            linkText.AppendLine("</script>");
            #endregion

            ltInput.Text = linkText.ToString();

            foreach (Control c in gvrp.Cells[0].Controls)
            {
                if (c.GetType().ToString().Equals("System.Web.UI.WebControls.PagerTable"))
                {
                    //在第一个TableCell之前加单元格
                    TableCell tc1 = new TableCell();
                    tc1.Controls.Add(lt);
                    c.Controls[0].Controls.AddAt(0, tc1);

                    //在最后一个TableCell之后加单元格
                    TableCell tc2 = new TableCell();
                    tc2.Controls.Add(ltInput);
                    c.Controls[0].Controls.AddAt(c.Controls[0].Controls.Count, tc2);
                }
            }
        }
    }
    #endregion

调用:

 

    int rowsCount = 0;
    protected void AdsList_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        rowsCount = e.AffectedRows;
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        NnllPublic.ReWriteGridViewPaging(rowsCount, GridView1);
    }

小楼宝宝的涂鸦花花(Imitater)的博客起名称骨测字皖ICP备06000023号-17