我想在转发器控制中进行分页。我使用 xml 作为带有 linq 的数据库。所以请给我建议如何做到这一点。我尝试了这段代码,但它不起作用
public int RowCount
{
get
{
return (int)ViewState["RowCount"];
}
set
{
ViewState["RowCount"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FetchData(5, 0);
}
else
{
PlaceHolder1.Controls.Clear();
CreatePagingControl();
}
}
private void FetchData(int take,int pagesize)
{
var doc = XDocument.Load(Server.MapPath("~/BlogContent.xml"));
var result = doc.Descendants("post").Where(x => x.Element("id") != null).Take(take).Skip(pageSize)
.Select(x => new
{
id = x.Element("id").Value,
title = x.Element("title").Value,
Description = x.Element("Discription").Value,
dt = x.Element("dt").Value,
mnt = x.Element("mnt").Value,
yr = x.Element("yr").Value,
postdate = x.Element("PostDate").Value
}).OrderByDescending(x => x.id);
PagedDataSource page = new PagedDataSource();
page.AllowPaging = true;
page.AllowCustomPaging = true;
page.DataSource = result;
page.PageSize = 10;
if (!IsPostBack)
{
RowCount = result.Count();
}
}
private void CreatePagingControl()
{
for (int i = 0; i < (RowCount / 10) + 1; i++)
{
LinkButton lnk = new LinkButton();
lnk.Click += new EventHandler(lbl_click);
lnk.ID = "lnkPage" + (i + 1).ToString();
lnk.Text = (i + 1).ToString();
PlaceHolder1.Controls.Add(lnk);
Label spacer = new Label();
spacer.Text = " ";
PlaceHolder1.Controls.Add(spacer);
}
}
void lbl_click(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
int currentPage = int.Parse(lnk.Text);
int take = currentPage * 10;
int skip = currentPage == 1 ? 0 : take - 10;
FetchData(take, skip);
}
当我使用此代码时,上一个和下一个按钮正在工作,但中继器上的数据没有改变