我开发了一个自定义数据分页器控件。它的“上一个”和“下一个”超链接按钮无法正常工作,我不明白为什么。
<asp:Repeater ID="rpt" runat="server">
<HeaderTemplate>
<asp:LinkButton ID="lnkbFirst" CommandName="<%#PageChangedItemCommand %>" CommandArgument="1" runat="server">«</asp:LinkButton>
<asp:LinkButton ID="lnkbPrevious" CommandName="<%#PageChangedItemCommand %>" CommandArgument="<%#PreviousPageIndex%>" runat="server"><</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton CommandName="<%#PageChangedItemCommand %>" CommandArgument="<%#Container.DataItem.ToString()%>" ID="p" runat="server" ><%#Container.DataItem.ToString()%> </asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkbNext" CommandName="<%#PageChangedItemCommand %>" CommandArgument="<%#NextPageIndex%>" runat="server">></asp:LinkButton>
<asp:LinkButton ID="lnkbLast" CommandName="<%#PageChangedItemCommand %>" CommandArgument="<%#PagesCount%>" runat="server">»</asp:LinkButton>
</FooterTemplate>
</asp:Repeater>
public partial class Pager : System.Web.UI.UserControl
{
public int CurrentPage { get; set; }
public int PageSize { get; set; }
public int DataItemsCount { get; set; }
private int _pagesCount;
public int PagesCount
{
get
{
if (DataItemsCount % PageSize == 0)
return _pagesCount = (DataItemsCount / PageSize);
else
return _pagesCount = ((DataItemsCount / PageSize) + 1);
}
private set
{
_pagesCount = value;
}
}
public event EventHandler<PageChangedEventArgs> PageChanged;
protected const string PageChangedItemCommand = "PageChanged";
private const string CurrentPageCssStyle = "font-weight:bold; font-size:15px;";
private void RaiseEvent(int currentPage)
{
if (PageChanged != null)
PageChanged(this, new PageChangedEventArgs(currentPage));
}
protected List<int> DataSource
{
get
{
List<int> pages = new List<int>();
for (int i = 1; i <= PagesCount; i++)
pages.Add(i);
return pages;
}
}
protected int NextPageIndex { get { return CurrentPage + 1; } }
protected int PreviousPageIndex { get { return CurrentPage -1; } }
public Pager()
{
CurrentPage = 1;
PageSize = 1;
DataItemsCount = 1;
}
protected void Page_Load(object sender, EventArgs e)
{
rpt.ItemCommand += new RepeaterCommandEventHandler(rpt_ItemCommand);
rpt.DataSource = DataSource;
rpt.DataBind();
//
if (!Page.IsPostBack)
{
CurrentPageSetCssStyle(CurrentPageCssStyle);
// SetupCommandArguments();
}
}
void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == PageChangedItemCommand)
{
CurrentPage = int.Parse(e.CommandArgument.ToString());
CurrentPageSetCssStyle(CurrentPageCssStyle);
// SetupCommandArguments();
RaiseEvent(CurrentPage);
}
}
private void CurrentPageSetCssStyle(string style)
{
foreach (RepeaterItem item in rpt.Items)
{
LinkButton lnkButton = item.FindControl("p") as LinkButton;
if (lnkButton != null)
{
if (lnkButton.CommandArgument == CurrentPage.ToString())
lnkButton.Attributes.Add("style", style);
}
}
// SetupCommandArguments();
}
void SetupCommandArguments()
{
LinkButton lnkbPrevious = rpt.Controls[0].Controls[0].FindControl("lnkbPrevious") as LinkButton;
if (lnkbPrevious != null)
lnkbPrevious.CommandArgument = (CurrentPage - 1).ToString();
LinkButton lnkbNext = rpt.Controls[rpt.Controls.Count - 1].Controls[0].FindControl("lnkbNext") as LinkButton;
if (lnkbPrevious != null)
lnkbPrevious.CommandArgument = (CurrentPage + 1).ToString();
}
}
我需要开发自己的寻呼灯控制。不要建议我使用其他分页控件。
过去几天我不得不做一个自定义寻呼机,但我的做法略有不同。
我选择使用两个中继器: 1. 这个是用来传呼的
<div class="searchResultsPaging" runat="server">
<asp:Repeater ID="Paging" runat="server">
<ItemTemplate>
<a href='<%# Eval("PageUrl") %>' class='<%# (Convert.ToBoolean(Eval("IsCurrent")) == true)? "pagingButtonOff" : "pagingButtonOn" %>'>
<%# Eval("PageText") %></a>
</ItemTemplate>
</asp:Repeater>
</div>
2.是为了我展示的物品
<asp:Repeater ID="Properties" runat="server">
<ItemTemplate>
<div class="propertyCard">
<div class="propertyTitle">
<a href='/property/<%# Eval("Id") %>'>
<%# Eval("Title") %></a></div>
<div class="propertySuburb">
<%# Eval("Suburb") %></div>
<img src='/_Remove/SamplePropertyImages/<%# Eval("PriorityImage") %>' width="190"
height="143" alt='Property for sale in <%# Eval("Suburb")%>' />
<div class="propertyFeatures">
<img src="/_Remove/Icons/Bed.gif" alt='<%# Eval("Suburb") %>, <%# Eval("City") %> property has <%# Eval("Bedrooms") %> bedrooms.' /><%# Eval("Bedrooms") %><img
src="/_Remove/Icons/Bath.gif" alt='<%# Eval("Suburb") %>, <%# Eval("City") %> property has <%# Eval("Bathrooms") %> bedrooms.' /><%# Eval("Bathrooms") %></div>
<div class="propertyPrice">
<%# Eval("Price","{0:###,##0.00}") %>
</div>
<a href="#">View Property Details</a>
</div>
</ItemTemplate>
</asp:Repeater>
在绑定期间,我的业务逻辑返回属性列表和总数。从那里可以轻松绑定和构建分页控件。
可能不适合您,因为您正在寻找下一页/后一页类型的功能。
问候, 雅克