为GridView的PagerTemplate动态生成页面链接按钮 [英] Dynamically generate page linkbuttons for the PagerTemplate of a GridView
问题描述
在MSDN页面上,找到GridView
控件的PagerTemplate
(重点是我的):
From the MSDN page for the PagerTemplate
of the GridView
control (emphasis mine):
通常,将按钮控件添加到寻呼机模板以执行寻呼操作.单击其CommandName属性设置为"Page"的按钮控件时,GridView控件将执行分页操作.按钮的 CommandArgument 属性确定要执行的分页操作的类型.
Typically, button controls are added to the pager template to perform the paging operations. The GridView control performs a paging operation when a button control with its CommandName property set to "Page" is clicked. The button's CommandArgument property determines the type of paging operation to perform.
- 下一页":导航到下一页.
- 上一页":导航到上一页.
- 第一页":导航到第一页.
- 最后一个":导航到最后一页.
- 整数值:导航到指定的页码.
- "Next": Navigates to the next page.
- "Prev": Navigates to the previous page.
- "First": Navigates to the first page.
- "Last": Navigates to the last page.
- Integer value: Navigates to the specified page number.
来源:由于next/prev/first/last按钮的静态特性,这些内容非常简单.
This stuff is pretty straightforward for the next/prev/first/last buttons because of their static nature.
<PagerTemplate>
<table>
<tr>
<td>
<asp:Button CommandName="Page" CommandArgument="First" Enabled="<%# Model.PageContext.HasPrevious %>" Text="First" runat="server" />
</td>
<td>
<asp:Button CommandName="Page" CommandArgument="Prev" Enabled="<%# Model.PageContext.HasPrevious %>" Text="Previous" runat="server" />
</td>
<td>
<asp:Button CommandName="Page" CommandArgument="Next" Enabled="<%# Model.PageContext.HasNext %>" Text="Next" runat="server" />
</td>
<td>
<asp:Button CommandName="Page" CommandArgument="Last" Enabled="<%# Model.PageContext.HasNext %>" Text="Last" runat="server" />
</td>
</tr>
</table>
</PagerTemplate>
另一方面,数字按钮的CommandArgument
必须是动态的,并且对于可以导航到的每个页面都是唯一的.我猜想我需要一个for
循环或一个转发器控件才能在用户的显示器上获得正确数量的页面链接.
On the other hand, the CommandArgument
for numeric buttons has to be dynamic and unique for each page that can be navigated to. I'm guessing I will need a for
-loop or a repeater control to get the right number of page links on the user's display.
我尝试过的一切似乎都没有效果.我的for循环代码甚至没有编译.
Nothing I've tried seems to just work. My for-loop code doesn't even compile.
<% for (int pageIndex = 0; pageIndex < Model.PageContext.PageCount; pageIndex++) { %>
<asp:LinkButton CommandName="Page" CommandArgument="<%= pageIndex %>" Text="<%= pageIndex + 1 %>" runat="server"/>
<% } %>
我的替代方法使用Repeater
控件并进行 编译,但是Repeater
控件本身为每个按钮处理ItemCommand
,从而防止了"Page" ItemCommand
事件的冒泡直到GridView
.
My alternative approach uses a Repeater
control and does compile, but the Repeater
control itself handles the ItemCommand
for each button, preventing the "Page" ItemCommand
events from bubbling up to the GridView
.
<asp:Repeater ItemType="System.Int32" SelectMethod="GetPages" runat="server">
<ItemTemplate>
<asp:LinkButton CommandName="Page" CommandArgument="<%# Item %>" Text="<%# Item + 1 %>" runat="server" />
</ItemTemplate>
</asp:Repeater>
每个按钮引发正确的事件,但是事件从未到达GridView
,因为它们由Repeater
控件在较低的级别上处理.我必须附加一个监听RepeaterCommandEventArgs
的事件处理程序,然后自己在GridView
上设置新的页面索引.
Each button raises the correct event, but the events never reach the GridView
because they are handled at a lower level by the Repeater
control. I have to attach an event handler that listens for RepeaterCommandEventArgs
and then set the new page index on the GridView
myself.
*深呼吸*
我可以添加数字页面按钮而不必自己整理活动吗?
Can I add numeric page buttons without having to wire up events myself?
基于上面的代码,我正在尝试实现的结果:
The result that I'm trying to achieve, based on the code above:
推荐答案
我发现了一种更简单的方法,与Framework 4.0兼容
I found an even simpler way, one that is framework 4.0 compatible
<asp:GridView ID="GridView_History" runat="server">
<PagerTemplate>
<asp:LinkButton ID="lnkPrev" runat="server" CommandName="Page" CommandArgument="Prev">Prev</asp:LinkButton>
<asp:Repeater ID="rptPagesHistory" OnItemDataBound="rptPagesHistory_ItemDataBound" runat="server" OnLoad="rptPagesHistory_Load">
<ItemTemplate>
<asp:LinkButton ID="lnkPageNumber" CommandName="Page" runat="server" OnClick="lnkPageNumberHistory_Click" />
</ItemTemplate>
</asp:Repeater>
<asp:LinkButton ID="lnkNext" runat="server" CommandName="Page" CommandArgument="Next">Next</asp:LinkButton>
</PagerTemplate>
</asp:GridView>
protected void rptPagesHistory_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
LinkButton lnkPageNumber = new LinkButton();
System.Int32 pageNumber = (System.Int32)e.Item.DataItem;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
lnkPageNumber = (LinkButton)e.Item.FindControl("lnkPageNumber");
lnkPageNumber.Text = pageNumber;
lnkPageNumber.CommandArgument = pageNumber - 1;
}
}
protected void rptPagesHistory_Load(object sender, EventArgs e)
{
Repeater rpt = (Repeater)sender;
rpt.DataSource = Enumerable.Range(1, GridView_History.PageCount);
rpt.DataBind();
}
protected void lnkPageNumberHistory_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
GridView_History.PageIndex = btn.CommandArgument;
GridView_History.DataBind();
}
这篇关于为GridView的PagerTemplate动态生成页面链接按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!