c# 中repeater分页问题
我用c#做一个文章列表页面,数量太多所以要用到分页:用GridView自带的分成,方便是很方便,就是很不灵活,所以我决定用Repeater来做
1,我先从数据库读出数据把它放在一个DataTable里边,再使用Repeater显示
我的问题:
在脚模板里的分页链接控件识别不到(lnkpre,lnknext,lnkcurpage提示找不到)
.aspx页面
<FooterTemplate>
<tr>
<td colspan="6"><div align="center">
<asp:HyperLink ID="lnkpre" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="lnknext" runat="server">上一页</asp:HyperLink>
<asp:label ID="lnkcurpage" Text="label1" runat="server"></asp:label>
</div></td>
</tr>
</table>
</FooterTemplate>
.CS页面
int currentPage;
if (Request.QueryString["page"] != null)
{
try
{
currentPage = Int32.Parse(Request.QueryString["page"].ToString());
}
catch
{
currentPage = 1;
}
}
else
{
currentPage = 1;
}
pds.CurrentPageIndex = currentPage - 1;
lnkcurpage.Text = "当前页:" + currentPage + " 页 共 " + pds.PageCount + " 页 ";
if (!pds.IsFirstPage)
{
lnkpre.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage - 1);
}
if (!pds.IsLastPage)
{
lnknext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage + 1);
}
--------------------编程问答-------------------- 分太少吗?不够的话可以加~ --------------------编程问答--------------------
--------------------编程问答--------------------
//應在Repeater的ItemDataBound事件中,對lnkpre和lnknext進行控制。
//大概的方法如下,LZ自已稍做修改
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
HyperLink lnkpre = e.Item.FindControl("lnkpre") as HyperLink;
if (lnkpre != null) lnkpre.NavigateUrl = "http://www.google.com";
HyperLink lnknext = e.Item.FindControl("lnknext") as HyperLink;
if (lnknext != null) lnknext.NavigateUrl = "http://www.baidu.com";
}
}
using System;--------------------编程问答-------------------- 参考下。这个适用于datalist 和Rp
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FSSoft.Web.UI;
using FSSoft.Common.DataBase.SqlserverClient;
using Business.User;
using System.Data.SqlClient;
public partial class News_admin_userManager : System.Web.UI.Page
{
protected int CurrentPage;//当前页数
protected int PageSize; //每页条数
protected int PageCount; //总页数
protected int RecordCount;//总条数
protected DataTable dt = new DataTable();
protected SQLTransaction SQLT = new SQLTransaction();
protected SQLselect sqls = new SQLselect();
public static string sql = "";
public static string count = "";
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
PageSize = 10;//每页6条记录
if (!Page.IsPostBack)
{
CurrentPage = 0;//当前页习惯设为0
ViewState["PageIndex"] = 0;//页索引也设为0
sql = "select * from dbo.F_User order by datetime desc ";
count = "select count(*) as count from dbo.F_User ";
Language.ChangeControlsText(LG_Panel);
this.BindShowF_Patient(); //不可以放在初始化条件之前就绑定,那样的话,如果仅有一页的数据,“下一页”页仍然显示
}
}
//计算总共有多少条记录
private int CalculateRecord(string strcount)
{
SQLreader sqlr = new SQLreader();
try
{
int recordCount = 0;
sqlr.Read_Form(count);
SqlDataReader sdr = sqlr.Sys_rdr;
if (sdr.Read())
{
recordCount = Int32.Parse(sdr["count"].ToString());
}
sqlr.CloseReader();
sqlr.ConClose();
return recordCount;
}
catch (Exception ex)
{
sqlr.CloseReader();
sqlr.ConClose();
throw new Exception(ex.Message);
}
}
/// <summary>
/// 绑定Repater并显示数据
/// </summary>
public void BindShowF_Patient()
{
int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
//计算总共有多少记录
RecordCount = CalculateRecord(count);
//计算总共有多少页
if (RecordCount % PageSize == 0)
{
PageCount = RecordCount / PageSize;
}
else
{
PageCount = RecordCount / PageSize + 1;
}
//this.lblRenNum.Text = RecordCount.ToString();//所有人数
this.TotalLbl.Text = PageCount.ToString();//显示总页数
ViewState["PageCount"] = PageCount;
// string sql = "select a.id,a.f_minzu,a.f_guoji,a.f_brithdate,a.f_shouji,a.f_dianhua,a.f_bingliNum,f_name_cn,b.f_jinqian,b.f_c易做图date,b.f_buildManid from F_Patient a, F_c易做图 b where a.f_name_cn=b.f_huanzhename order by b.f_datetime desc";
DataSet ds = new DataSet();
ds = sqls.Sys_DS;
sqls.Fill_DS(sql, "F_User", StartIndex, PageSize); //这是sda.Fill方法的第一次重载,里面的变量分别是数据集DataSet ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName
this.dlHuanzhe.DataSource = ds.Tables["F_User"].DefaultView;
this.dlHuanzhe.DataBind();
this.PreviousLB.Enabled = true;
this.NextLB.Enabled = true;
this.EndLB.Enabled = true;
this.FirstLB.Enabled = true;
if (CurrentPage == (PageCount - 1))
{
this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
this.EndLB.Enabled = false;
}
if (CurrentPage == 0)
{
this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
this.FirstLB.Enabled = false;
}
if (PageCount <= 1)
{
this.PreviousLB.Enabled = false;
this.NextLB.Enabled = false;
this.FirstLB.Enabled = false;
this.EndLB.Enabled = false;
}
this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数
////原来你是因为那个dropdownlist没有取到数据的原因加上这句就能取到数据了
PageCount = int.Parse(this.ViewState["PageCount"].ToString());
if (PageCount != drpPage.Items.Count)
{
drpPage.Items.Clear();
for (int i = 1; i <= PageCount; i++)
{
ListItem LI = new ListItem(i.ToString(), i.ToString());
drpPage.Items.Add(LI);
}
}
//如果输入错别字或者没有查出数据则让下拉框为0,以前没有这句的时候下拉框里面是空的!
if (PageCount == 0)
{
drpPage.Items.Insert(0, new ListItem("0", "-1"));
lblError.Text = "暂无数据!";
this.dlHuanzhe.Visible = false;
}
sqls.ConClose();
}
/// <summary>
/// 分页事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件
{
CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
PageCount = (int)ViewState["PageCount"];//获得总页数
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch (cmd)
{
case "prev"://上一页
if (CurrentPage > 0) CurrentPage--;
this.drpPage.SelectedIndex = CurrentPage;
break;
case "next":
if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
this.drpPage.SelectedIndex = CurrentPage;
break;
case "first"://第一页
CurrentPage = 0;
this.drpPage.SelectedIndex = CurrentPage;
break;
case "end"://最后一页
CurrentPage = PageCount - 1;
this.drpPage.SelectedIndex = CurrentPage;
break;
}
ViewState["PageIndex"] = CurrentPage;//获得当前页
this.BindShowF_Patient();//重新将DataList绑定到数据库
}
/// <summary>
/// 下拉框选择页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void drpPage_SelectedIndexChanged(object sender, EventArgs e)
{
//应该是当前页数=下拉框的值
CurrentPage = drpPage.SelectedIndex;
this.BindShowF_Patient();
}
/// <summary>
/// 删除用户
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDelte_Click(object sender, EventArgs e)
{
//对有没有选中进行判断直接按删除按钮则报错
bool strDrive = false;
try
{
SQLT.SetSQLBeginTrans();
for (int i = 0; i < dlHuanzhe.Items.Count; i++)
{
CheckBox CB = (CheckBox)dlHuanzhe.Items[i].FindControl("chk");
if (CB.Checked)
{
strDrive = true;
//隐藏传递ID用一个datatlist就是Parent一个
string delID = ((HiddenField)dlHuanzhe.Items[i].FindControl("id")).Value;
SQLT.Operate("delete from dbo.F_User where userid='" + delID + "'");
}
}
SQLT.SetSQLCommitTrans();
SQLT.ConClose();
}
catch
{
SQLT.SetSQLRollBack();
Response.Write("<script>alert('删除失败。')</script>");
SQLT.ConClose();
}
if (!strDrive)//判断选中没有
{
Response.Write("<script>alert('请选择要删除的数据。')</script>");
}
else//删除后重新绑定
{
//删除后进行重新取总人数
//this.lblRenNum.Text = CalculateRecord(count).ToString();
this.BindShowF_Patient();
}
}
}
补充:.NET技术 , C#