GridView的PagerTemplate分页(二)
1、开启GridView的分页功能AllowPaging为Ture.
在Html源设置GridView的PagerTemplate:
<asp:GridView ID="gvCustom" runat="server" BorderColor="Gray" Height="20px" Width="98%" AllowPaging="True" AutoGenerateColumns="False" OnPageIndexChanging="gvCustom_PageIndexChanging" OnRowDataBound="gvCustom_RowDataBound" EmptyDataText="没有符合查询条件的数据!" OnDataBound="gvCustom_DataBound" OnRowDeleting="gvCustom_RowDeleting">
<EditRowStyle BackColor="SeaGreen" />
<SelectedRowStyle BorderColor="SeaGreen" />
<HeaderStyle BackColor="#718BD6" Font-Size="Small" ForeColor="White" Height="20px" />
<PagerSettings Mode="NumericFirstLast" />
<RowStyle Height="16px" />
<AlternatingRowStyle BackColor="#EDF5FF" />
<Columns>
<asp:BoundField DataField="CustomID" HeaderText="客户ID" Visible="False" />
<asp:BoundField DataField="CustomName" HeaderText="客户名称" />
<asp:BoundField DataField="CustomType" HeaderText="客户类型" />
<asp:BoundField DataField="UserName" HeaderText="销售员" />
<asp:BoundField DataField="LinkMan" HeaderText="联系人" />
<asp:BoundField DataField="Tel" HeaderText="联系电话" />
<asp:HyperLinkField DataNavigateUrlFields="CustomID" DataNavigateUrlFormatString="CustomInfo.aspx?CustomID={0}"
HeaderText="查看" Text="详细信息" />
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:ImageButton ID="lbtnDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/drop.gif" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataRowStyle ForeColor="Red" />
<PagerTemplate>
页面大小:<%# this.gvCustom.PageSize %> 条记录
<asp:LinkButton ID="lbtnPrev" Text="上一页" CommandArgument="p" OnCommand="LinkPageChanged" runat="server"></asp:LinkButton>
<asp:LinkButton ID="lbtnNext" Text="下一页" CommandArgument="n" OnCommand="LinkPageChanged" runat="server"></asp:LinkButton>
转到
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropPageChanged">
</asp:DropDownList>页
页面信息:第<%# this.gvCustom.PageIndex + 1 %>页/共<%# this.gvCustom.PageCount%>页
</PagerTemplate>
</asp:GridView>
添加后台事件(共3个事件,Copy一下代码即可):
protected void LinkPageChanged(object sender, EventArgs e)
{
LinkButton lb = sender as LinkButton;
//上一页和下一页
if (lb.CommandArgument == "p")
{
if (this.gvCustom.PageIndex > 0)
this.gvCustom.PageIndex--;
}
else if (lb.CommandArgument == "n")
{
this.gvCustom.PageIndex++;
}
this.BindCustomList();
}
protected void DropPageChanged(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
this.gvCustom.PageIndex = int.Parse(ddl.SelectedValue);
this.BindCustomList();
}
protected void gvCustom_DataBound(object sender, EventArgs e)
{
//上一页按钮是否可用
LinkButton lbp = this.gvCustom.BottomPagerRow.Cells[0].FindControl("lbtnPrev") as LinkButton;
if (this.gvCustom.PageIndex-1 < 0)
lbp.Enabled = false;
else
lbp.Enabled = true;
//下一页按钮是否可用
LinkButton lbn = this.gvCustom.BottomPagerRow.Cells[0].FindControl("lbtnNext") as LinkButton;
if (this.gvCustom.PageIndex + 1 < this.gvCustom.PageCount )
lbn.Enabled = true;
else
lbn.Enabled = false;
//绑定总页数
DropDownList ddl = this.gvCustom.BottomPagerRow.Cells[0].FindControl("ddl") as DropDownList;
for (int i = 0; i < this.gvCustom.PageCount; i++)
{
ddl.Items.Add(new ListItem((i + 1).ToString(), i.ToString()));
}
ddl.SelectedIndex = this.gvCustom.PageIndex;
}
试试看效果怎么样吧:)
浙公网安备 33010602011771号