第一次使用ASP.NET,遇到的问题实在太多了,前几天想要实现GridView中的分页效果,知道了自带的PagerSettings功能,但是发现它不能同时有上下页和页码的功能,后来知道了PagerTemplate自定义模板的功能,根据以下文章的方法进行了设置:
代码如下:可以利用linkbutton 的参数commandargument和commandname来实现上下页和首尾页的功能。
<PagerTemplate>
<table width="100%">
<tr>
<td style="text-align: right">
第<asp:Label ID="lblPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
共/<asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>' />页
<asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First"
CommandName="Page" Text="首页" />
<asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" CommandArgument="Prev"
CommandName="Page" Text="上一页" />
<asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandArgument="Next"
CommandName="Page" Text="下一页" />
<asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" CommandArgument="Last"
CommandName="Page" Text="尾页" />
<asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1"
CommandName="Page" Text="GO" /><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->
</td>
</tr>
</table>
</PagerTemplate>
但是还是没有页码的按钮,然后我用了如下的方法,在模板中用FOR添加了多个commandargument为数字的linkbutton 。这样在效果上是有了页码按钮了,可是点击的参数总是为空的,也就是说CommandArgument="<%=i+1 %>" 中不可以用<%=%>来调用i的值,可是使用Response.Write的方法有不可以写asp的控件,那如何可以将这个页号的参数传到后台呢?
Code
。。。
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandArgument="Prev" CommandName="Page" Text="上一页" />
<% for (int i = 0; i <= this.grvPage.PageCount; i++)
{
if (i == this.grvPage.PageIndex)
Response.Write("<span style='background-color:Blue; color:#ffffff'>" + (i+1) + "</span>");
else %>
<asp:LinkButton runat="server" CausesValidation="False" CommandArgument="<%=i+1 %>" CommandName="Page" ><%=i+1 %></asp:LinkButton>
<% Response.Write(" ");
} %>
<asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandArgument="Next" CommandName="Page" Text="下一页" />
。。。
呵呵,当然,最后还是没有找到方法。不过这里用了另外的一种方法实现了效果,就是页面GET(QueryString)传数据的方法。下面是效果图。
[1] [2]