#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);
}