我正在尝试使用PagedDataSource在转发器中实现分页。问题是当我点击“下一步”按钮时页面仍然相同,它不会加载其他项目。我发现viewstate总是返回null,我不知道这是不是问题。
这是c#代码:
public partial class Users
{
private int _pageSize;
PagedDataSource page = new PagedDataSource();
private const bool blnAllowPaging = true;
private const int iPageSize = 3;
protected void Page_Load(object sender, EventArgs e)
{
// if page is loaded for the first time call BindRepeater()
if (!Page.IsPostBack)
{
// Load the table
BindRepeater();
}
}
protected void lnkFirst_Click(object sender, EventArgs e)
{
// Set viewstate variable to the first page
CurrentPage = 0;
BindRepeater();
}
protected void lnkPrevious_Click(object sender, EventArgs e)
{
// Set viewstate variable to the previous page
CurrentPage -= 1;
BindRepeater();
}
protected void lnkNext_Click(object sender, EventArgs e)
{
// Set viewstate variable to the next page
CurrentPage += 1;
BindRepeater();
}
protected void lnkLast_Click(object sender, EventArgs e)
{
// Set viewstate variable to the last page
CurrentPage = page.PageCount - 1;
BindRepeater();
}
private void FillPagesDropDownList(int iTotalPages)
{
ddlpageNumbers.Items.Clear();
for (int i = 1; i <= iTotalPages; i++)
{
ddlpageNumbers.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
}
protected void ddlpageNumbers_SelectedIndexChanged(object sender, EventArgs e)
{
CurrentPage = ddlpageNumbers.SelectedIndex;
BindRepeater();
}
private int CurrentPage
{
get
{
EnableViewState = true;
object obj = ViewState["CurrentPage"];
return (obj == null) ? 0 : (int)obj;
}
set
{
this.ViewState["CurrentPage"] = value;
}
}
private void BindRepeater()
{
page.AllowPaging = blnAllowPaging;
page.PageSize = iPageSize;
lblTotalPages.Text = page.PageCount.ToString();
FillPagesDropDownList(page.PageCount);
// Disable buttons if necessary
lnkFirst.Enabled = !page.IsFirstPage;
lnkPrevious.Enabled = !page.IsFirstPage;
lnkNext.Enabled = !page.IsLastPage;
lnkLast.Enabled = !page.IsLastPage;
// Set the PagedDataSource's current page
page.CurrentPageIndex = CurrentPage;
ddlpageNumbers.SelectedIndex = page.CurrentPageIndex;
List<USERS> users = GetUsers(); // list of users taken from db
page.DataSource = users;
page.DataBind();
}
}
这是asp.net代码:
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<style type="text/css">
.style1
{
width: 528px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Repeater ID="m_SupportersRepeater" runat="server" OnItemDataBound="SupportersRepeater_OnItemDataBound">
<HeaderTemplate>
<table border="1" width="90%">
<tr>
<td>Name</td>
<td>Family Name</td>
<td>Age</td>
<td>City</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "FIRSTNAME")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "LASTNAME")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "AGE")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "CITY")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<table style="margin-left: 214px">
<tr align="center">
<td colspan="4" class="style1">
<asp:LinkButton ID="lnkFirst" runat="server" ForeColor="Black" Text="First" onclick="lnkFirst_Click" ></asp:LinkButton>
<asp:LinkButton ID="lnkPrevious" runat="server" ForeColor="Black" Text="Previous" OnClick="lnkPrevious_Click" > </asp:LinkButton>
Page
<asp:DropDownList ID="ddlpageNumbers" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlpageNumbers_SelectedIndexChanged"> </asp:DropDownList> of
<asp:Label ID="lblTotalPages" runat="server"> </asp:Label> Pages.
<asp:LinkButton ID="lnkNext" runat="server" ForeColor="Black" Text="Next" Onclick="lnkNext_Click" ></asp:LinkButton>
<asp:LinkButton ID="lnkLast" runat="server" ForeColor="Black" Text="Last" Onclick="lnkLast_Click" ></asp:LinkButton>
</td>
</tr>
</table>
</asp:Content>
答案 0 :(得分:0)
在BindRepeater方法中,尝试将最后三行代码(检索数据并将其绑定到PagedDataSource成员)移动到方法的顶部,以便在开始评估其属性之前填充PagedDataSource对象。我认为在方法结束时加载数据会将page.CurrentPageIndex属性重置为零。所以首先加载PagedDataSource,在PagedDataSource上设置分页属性,然后将Repeater的DataSource属性设置为PagedDataSourceObject。