您的位置:首页 > 其它

分页:所有的数据绑定控件都可以用的

2011-07-15 11:06 225 查看
自己写了一个分页方法,数据绑定控件可以是Repeater、DataList、。。。等等都行,因为原理是每次重新获取页面所需的数据,然后再重新绑定。

后台代码:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}

private void BindData()
{
Article a = new Article();
pageCount = 10; //设置每页记录条数
totalCount = a.GetTotalCount(); //获取记录总数
rpArticle.DataSource = a.GetPageData(GetCurrentIndex(), pageCount);//获取分页数据
rpArticle.DataBind();
ResetButtonStatus(pageCount,totalCount);
}

#region 分页处理

//使用例子
//private void BindData()
//{
//    Article a = new Article();
//    pageCount = 10; //设置每页记录条数
//    totalCount = a.GetTotalCount(); //获取记录总数
//    rpArticle.DataSource = a.GetPageData(GetCurrentIndex(), pageCount);//获取该页所需的数据
//    rpArticle.DataBind();
//    ResetButtonStatus(pageCount, totalCount);
//}

//获取分页数据SQL格式,参数@PageIndex:第几页数据,参数@PageCount:每页的记录条数
//sb.Append("select * from  ");
//sb.Append("(select ROW_NUMBER() over(order by ArticleID desc) as row_number,T_Article.Title  ");
//sb.Append("from T_Article ) t ");
//sb.Append("where row_number > (@PageIndex-1)*@PageCount  ");
//sb.Append("and row_number <= (@PageIndex-1)*@PageCount + @PageCount");

/// <summary>
/// 每页的记录条数
/// </summary>
public int pageCount { get; set; }

/// <summary>
/// 记录总数
/// </summary>
public int totalCount { get; set; }

/// <summary>
/// 获取当前页
/// </summary>
/// <returns></returns>
private int GetCurrentIndex()
{
int index = 1;
if (ViewState["currentIndex"] == null)
{
ViewState["currentIndex"] = index;
}
else
{
index = Convert.ToInt32(ViewState["currentIndex"]);
}
return index;
}

/// <summary>
/// 重置按钮
/// </summary>
/// <param name="pageCount"></param>
private void ResetButtonStatus(int pageCount,int totalCount)
{
lblTotal.Text = totalCount.ToString();
int maxPage = totalCount % pageCount == 0 ? totalCount / pageCount : totalCount / pageCount + 1;
lblTotalPage.Text = maxPage.ToString();

ddlCurrentPage.Items.Clear();
for (int i = 1; i <= maxPage; i++)
{
ddlCurrentPage.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddlCurrentPage.SelectedValue = GetCurrentIndex().ToString();
if (GetCurrentIndex() == maxPage)
{
lbtnNext.Enabled = false;
//lbtnNext.Text = "已到末页";
lbtnLast.Enabled = false;
}
else
{
lbtnNext.Enabled = true;
lbtnNext.Text = "下一页";
lbtnLast.Enabled = true;
}

if (GetCurrentIndex() == 1)
{
lbtnPrev.Enabled = false;
//lbtnPrev.Text = "已到首页";
lbtnFirst.Enabled = false;
}
else
{
lbtnPrev.Enabled = true;
lbtnPrev.Text = "上一页";
lbtnFirst.Enabled = true;
}

}

//上一页
protected void lbtnPrev_Click(object sender, EventArgs e)
{
ViewState["currentIndex"] = Convert.ToInt32(ViewState["currentIndex"]) - 1;
BindData();
}
//下一页
protected void lbtnNext_Click(object sender, EventArgs e)
{
ViewState["currentIndex"] = Convert.ToInt32(ViewState["currentIndex"]) + 1;
BindData();
}
//页面跳转
protected void ddlCurrentPage_SelectedIndexChanged(object sender, EventArgs e)
{
ViewState["currentIndex"] = Convert.ToInt32(ddlCurrentPage.SelectedItem.Text);
BindData();
}
//首页
protected void lbtnFirst_Click(object sender, EventArgs e)
{
ViewState["currentIndex"] = 1;
BindData();
}
//尾页
protected void lbtnLast_Click(object sender, EventArgs e)
{
ViewState["currentIndex"] = Convert.ToInt32(lblTotalPage.Text);
BindData();
}
#endregion


  

前台html:



<form id="form1" runat="server">
<div>
<asp:Repeater ID="rpArticle" runat="server" EnableViewState="False">
<ItemTemplate>
<%#Eval("Title") %><br />
</ItemTemplate>
</asp:Repeater>
</div>
<div id="pageshow" style="font-size: 14px; color: #0C0C0C; font-weight: 500;">
<asp:LinkButton ID="lbtnFirst" runat="server" OnClick="lbtnFirst_Click">首页</asp:LinkButton>
 <asp:LinkButton ID="lbtnPrev" runat="server" OnClick="lbtnPrev_Click">上一页</asp:LinkButton>
 <asp:LinkButton ID="lbtnNext" runat="server" OnClick="lbtnNext_Click">下一页</asp:LinkButton>
 <asp:LinkButton ID="lbtnLast" runat="server" OnClick="lbtnLast_Click">尾页</asp:LinkButton>
 <asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCurrentPage_SelectedIndexChanged">
</asp:DropDownList>
/<asp:Label ID="lblTotalPage" runat="server"></asp:Label>
 总共
<asp:Label ID="lblTotal" runat="server"></asp:Label>
 条记录</div>
</form>


  


效果图:



PS:

有人问Article类是干什么的,其实这个类只是封装了对我的数据表Article的操作而已,Article类我只写了两个方法,一个是获取分页数据GetPageData

/// <summary>
/// 获取分页数据
/// </summary>
/// <param name="PageIndex">页索引</param>
/// <param name="PageCount">页大小</param>
/// <returns>DataSet</returns>
public DataSet GetPageData(int PageIndex, int PageCount)
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["CollegeWebConnectionString"].ToString();
con.Open();
SqlCommand cmd = con.CreateCommand();
StringBuilder sb = new StringBuilder();

sb.Append("select * from  ");
sb.Append("(select ROW_NUMBER() over(order by ArticleID desc) as row_number,T_Article.Title  ");
sb.Append("from T_Article ) t ");
sb.Append("where row_number > (@PageIndex-1)*@PageCount  ");
sb.Append("and row_number <= (@PageIndex-1)*@PageCount + @PageCount");

cmd.CommandText = sb.ToString();
cmd.Parameters.Add(new SqlParameter("@PageIndex", PageIndex));
cmd.Parameters.Add(new SqlParameter("@PageCount", PageCount));

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}


  另外一个是获取数据的总数:GetTotalCount()

public int GetTotalCount()
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["CollegeWebConnectionString"].ToString();
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "select count(*) from T_Article";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐