程序控制DataGrid的分页
public string cPage;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
dataBind();
}
}
public void dataBind()
{
cPage="1";
if(Request.QueryString["Page"]!=null)
{
cPage=Request.QueryString["Page"];
}
dgTitle.CurrentPageIndex=Int32.Parse(cPage)-1;
dgTitle.DataSource=bind.tableBind(Convert.ToInt32(Request.QueryString["id"]));
dgTitle.DataBind();
ShowStates();
}
private void ShowStates()
{
PageInfo.Text="页次:<font color='#FF0000'>"+(dgTitle.CurrentPageIndex+1)+"</font>/<font color='#FF0000'>"+dgTitle.PageCount+"</font>每页<font color='#FF0000'>"+dgTitle.PageSize+"</font>个主题 ";
JumpTo.Text="" + (dgTitle.CurrentPageIndex+1);
int temp=dgTitle.CurrentPageIndex/30;
if(temp==0)
{
PageLink.Text+="<A href='showtitle.aspx?Cid="+Request.QueryString["Cid"]+"&Page=1' title=首页><font face=webdings>9</font></a> ";
}
else
{
PageLink.Text+="<A href='ListArticle.aspx?Cid="+Request.QueryString["Cid"]+"&Page=1' title=首页><font face=webdings>9</font></a> <A href='showtitle.aspx?Cid="+Request.QueryString["Cid"]+"&Page="+((temp+1)*10-10)+"' title=上十页><font face=webdings>7</font></a> ";
}
for(int i=(temp+1)*10-9;i<=(temp+1)*30;i++)
{
if(i>dgTitle.PageCount)
{
break;
}
else if(i==dgTitle.CurrentPageIndex+1)
{
PageLink.Text+="<FONT color='red'><b>"+i.ToString()+"</b></Font> ";
}
else
{
PageLink.Text+="<A href='showtitle.aspx?Cid="+Request.QueryString["id"]+"&Page="+i+"'><b>"+i.ToString()+"</b></a> ";
}
}
if(temp==(dgTitle.PageCount-1)/30)
{
PageLink.Text+="<A href='showtitle.aspx?Cid="+Request.QueryString["id"]+"&Page="+dgTitle.PageCount+"' title=尾页><font face=webdings>:</font></a>";
}
else
{
PageLink.Text+="<A href='showtitle.aspx?Cid="+Request.QueryString["id"]+"&Page="+((temp+1)*10+1)+"' title=下十页><font face=webdings>8</font></a> <A href='showtitle.aspx?id="+Request.QueryString["id"]+"&Page="+dgTitle.PageCount+"' title=尾页><font face=webdings>:</font></a>";
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgTitle_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgTitle.CurrentPageIndex=e.NewPageIndex;
dataBind();
}
private void Go_NewPage()
{
if (Page.IsValid)
{
string Topage = JumpTo.Text;
if (Topage != "")
{
if (Int32.Parse(Topage) <= (dgTitle.PageCount) && Int32.Parse(Topage)>0)
{
Response.Redirect("showtitle.aspx?id="+Request.QueryString["id"]+"&Page="+Int32.Parse(Topage));
}
else
{
Response.Redirect("showtitle.aspx?id="+Request.QueryString["id"]+"&Page=1");
}
}
}
}
private void btnGo_Click(object sender, System.EventArgs e)
{
Go_NewPage();
}
}
}