DataGrid自定义分页总结
html中
<asp:linkbutton id="btnFirst" οnclick="PageButtonClick" runat="server" Font-Name="verdana" CommandArgument="0">最前页
</asp:linkbutton> <asp:linkbutton id="btnPrev" οnclick="PageButtonClick" runat="server" CommandArgument="prev">上一页
</asp:linkbutton> <asp:linkbutton id="btnNext" οnclick="PageButtonClick" runat="server" CommandArgument="next">下一页
</asp:linkbutton> <asp:linkbutton id="btnLast" οnclick="PageButtonClick" runat="server" CommandArgument="last">最后页
</asp:linkbutton>
总共有
<asp:label id="lblcount" runat="server" ForeColor="Red"></asp:label> 条
记录<asp:label id="lblCurrentIndex" runat="server"></asp:label>
<asp:label id="lblPageCount" runat="server"></asp:label></TD>
CS后台文件中
protected void GridBind()
{
.........
DataGrid1.DataBind();
lblcount.Text=ds.Tables[0].Rows.Count.ToString();
conn.Close();
ShowStats();
setPage();
}
public void setPage()
{
if(DataGrid1.PageCount<= 1 )//小于等于一页
{
btnFirst.Enabled = false;
btnPrev.Enabled = false;
btnNext.Enabled = false;
btnLast.Enabled = false;
}
else //有多页
{
if(DataGrid1.CurrentPageIndex== 0 )//当前为第一页
{
btnFirst.Enabled = false;
btnPrev.Enabled = false;
btnNext.Enabled = true;
btnLast.Enabled = true;
}
else if(DataGrid1.CurrentPageIndex==DataGrid1.PageCount-1)//当前为最后页
{
btnFirst.Enabled = true;
btnPrev.Enabled = true;
btnNext.Enabled = false;
btnLast.Enabled = false;
}
else //中间页
{
btnFirst.Enabled = true;
btnPrev.Enabled = true;
btnNext.Enabled = true;
btnLast.Enabled = true;
}
}
}
public void PageButtonClick(object sender,EventArgs e)
{
string arg=((LinkButton)sender).CommandArgument.ToString();
switch(arg)
{
case "next":
if(DataGrid1.CurrentPageIndex<(DataGrid1.PageCount-1))
{
DataGrid1.CurrentPageIndex+=1;
}
break;
case "prev":
if(DataGrid1.CurrentPageIndex>0)
{
DataGrid1.CurrentPageIndex-=1;
}
break;
case "last":
DataGrid1.CurrentPageIndex=(DataGrid1.PageCount-1);
break;
default:
DataGrid1.CurrentPageIndex=System.Convert.ToInt32(arg);
break;
}
GridBind();
ShowStats();
}
private void ShowStats()
{
//显示当前页面是第几页
lblCurrentIndex.Text = "第 " + (DataGrid1.CurrentPageIndex + 1).ToString() + " 页";
//显示总页数
lblPageCount.Text = "总共 " + DataGrid1.PageCount.ToString() + " 页";
}