1:GridView
<PagerTemplate>
<div style="text-align: right;">
<asp:LinkButton ID="cmdFirstPage" runat="server" CommandName="Page" CommandArgument="First">
首页
</asp:LinkButton>
<asp:LinkButton ID="cmdPreview" runat="server" CommandArgument="Prev" CommandName="Page">
前页</asp:LinkButton>
<asp:LinkButton ID="cmdNext" runat="server" CommandName="Page" CommandArgument="Next">
后页
</asp:LinkButton>
<asp:LinkButton ID="cmdLastPage" runat="server" CommandArgument="Last" CommandName="Page">
尾页</asp:LinkButton>
第
<asp:Label ID="lblcurPage" runat="server"></asp:Label>页/共
<asp:Label ID="lblPageCount" runat="server"></asp:Label>页
转<asp:TextBox ID="txtGoPage" runat="server"
Width="32px" CssClass="inputmini" AutoPostBack="True"
ontextchanged="txtGoPage_TextChanged"></asp:TextBox>页
</div>
</PagerTemplate>
2:存储过程
Create PROCEDURE [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 主键字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(100) -- 临时变量(查询条件过长时可能会出错,可修改100为1000)
declare @strOrder varchar(400) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp =''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
if @IsReCount != 0
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere
exec (@strSQL)
3:batabind(int PageIndex, int PageSize, string strSql)
/// <summary>
/// 功能:分页控
/// 作 者 : zqf
/// 完成日期: 2007-05-28
/// 版权所有:
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="PageSize">每页几行</param>
/// <param name="strSql">条件</param>
protected void batabind(int PageIndex, int PageSize, string strSql)
{
DAL.DB_TB_Test pageListTest = new DAL.DB_TB_Test();
DataSet dsPage=pageListTest.GetListPage(PageSize,PageIndex , "1=1");
GridView1.DataSource = dsPage.Tables[0].DefaultView;
GridView1.DataBind();
//GetListPro() 取全部记录
int recordTotal = pageListTest.GetListPro().Tables[0].Rows.Count;
int totalPage=0;
if (recordTotal % PageSize > 0)
{
totalPage = pageListTest.GetListPro().Tables[0].Rows.Count / PageSize + 1;
}
else
{
totalPage = pageListTest.GetListPro().Tables[0].Rows.Count / PageSize;
}
GridView1.BottomPagerRow.Visible = true;
Label lbPCount = (Label)GridView1.BottomPagerRow.FindControl("lblPageCount");
lbPCount.Text = totalPage.ToString();
Label lbcurPage = (Label)GridView1.BottomPagerRow.FindControl("lblcurPage");
lbcurPage.Text = PageIndex.ToString();
// GridView1.PageIndex = PageIndex;
}
4:protected void Page_Load(object sender, EventArgs e)
{
int PageIndex=1;
if (!IsPostBack)
{
batabind(PageIndex, 10, "");
}
}
5: protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int PageIndex = GridView1.PageIndex;
batabind((PageIndex+1), 10, "");
}
6:protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Label lbPCount = (Label)GridView1.BottomPagerRow.FindControl("lblPageCount");
int pageCount = Convert.ToInt32(lbPCount.Text.Trim().ToString());
Label lbcurPage = (Label)GridView1.BottomPagerRow.FindControl("lblcurPage");
int curPage = Convert.ToInt32(lbcurPage.Text.Trim().ToString());
if (e.CommandName == "Page")
{
string arg = e.CommandArgument.ToString();
GridView1.PageIndex = curPage-1;
switch (arg)
{
case "Next":
if (GridView1.PageIndex < (pageCount - 1))
{
GridView1.PageIndex += 1;
}
break;
case "Prev":
if (GridView1.PageIndex > 0)
{
GridView1.PageIndex -= 1;
}
break;
case "Last":
GridView1.PageIndex = (pageCount - 1);
break;
case "First":
GridView1.PageIndex = 0;
break;
//default:
// GridView1.PageIndex = System.Convert.ToInt32(arg);
// break;
}
}
}
7:
protected void txtGoPage_TextChanged(object sender, EventArgs e)
{
Label lbPCount = (Label)GridView1.BottomPagerRow.FindControl("lblPageCount");
int pageCount = Convert.ToInt32(lbPCount.Text.Trim().ToString());
int PageIndex=Convert.ToInt32(((TextBox)sender).Text) - 1;
if ((PageIndex + 1) > pageCount)
{
Response.Write("out pageCount !");
}
else
{
batabind((PageIndex + 1), 10, "");
}
}
########################################
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (GridView1.Rows.Count == 1 && GridView1.PageIndex == GridView1.PageCount - 1)
{
if (GridView1.PageIndex - 1 > 1)
{
GridView1.PageIndex = GridView1.PageIndex - 1;
}
else
{
GridView1.PageIndex = 0;
}
}
int del_int = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
DAL.DB_TB_Test delTest = new DAL.DB_TB_Test();
delTest.DeletePro(del_int);
//batabind();
Label lbcurPage = (Label)GridView1.BottomPagerRow.FindControl("lblcurPage");
int curPage = Convert.ToInt32(lbcurPage.Text.Trim().ToString());
GridView1.PageIndex = curPage - 1;
int PageIndex = GridView1.PageIndex;
batabind((PageIndex + 1), 10, "");
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Label lbcurPage = (Label)GridView1.BottomPagerRow.FindControl("lblcurPage");
int curPage = Convert.ToInt32(lbcurPage.Text.Trim().ToString());
GridView1.PageIndex = curPage - 1;
int PageIndex = GridView1.PageIndex;
batabind((PageIndex + 1), 10, "");
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Label lbcurPage = (Label)GridView1.BottomPagerRow.FindControl("lblcurPage");
int curPage = Convert.ToInt32(lbcurPage.Text.Trim().ToString());
GridView1.PageIndex = curPage - 1;
int PageIndex = GridView1.PageIndex;
batabind((PageIndex + 1), 10, "");
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DAL.DB_TB_Test updateTest = new DAL.DB_TB_Test();
int id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text.ToString());
string userName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim().ToString();
string passWord = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim().ToString();
Model.TB_Test editTest = new Model.TB_Test();
editTest.Id = id;
editTest.Username = userName;
editTest.Password = passWord;
updateTest.UpdatePro(editTest);
GridView1.EditIndex = -1;
Label lbcurPage = (Label)GridView1.BottomPagerRow.FindControl("lblcurPage");
int curPage = Convert.ToInt32(lbcurPage.Text.Trim().ToString());
GridView1.PageIndex = curPage - 1;
int PageIndex = GridView1.PageIndex;
batabind((PageIndex + 1), 10, "");
}