

<asp:GridView ID="gvProject" runat="server" BorderColor="Gray" Height="20px" Width="98%" AllowPaging="True" AutoGenerateColumns="False" EmptyDataText="没有符合查询条件的数据!" OnDataBound="gvProject_DataBound" OnPageIndexChanging="gvProject_PageIndexChanging" OnRowDataBound="gvProject_RowDataBound" OnRowDeleting="gvProject_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="ProjectID" HeaderText="项目ID" Visible="False" />
<asp:BoundField DataField="ProjectName" HeaderText="项目名称" />
<asp:BoundField DataField="ProjectStatus" HeaderText="项目状态" />
<asp:BoundField DataField="ssjd" HeaderText="实施进度" >
<ItemStyle ForeColor="Red" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ssBeginDate" HeaderText="实施开始时间" DataFormatString="{0:d}" HtmlEncode="False" />
<asp:BoundField DataField="ssEndDate" HeaderText="要求结束时间" DataFormatString="{0:d}" HtmlEncode="False" />
<asp:BoundField DataField="UserName" HeaderText="项目负责人" />
<asp:HyperLinkField DataNavigateUrlFields="ProjectID" DataNavigateUrlFormatString="ProjectInfo.aspx?ProjectID={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>
当前第:
<asp:Label ID="LabelCurrentPage" runat="server"
Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
页/共:
<asp:Label ID="LabelPageCount" runat="server"
Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label> 页
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton>
转到第
<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" />
</PagerTemplate>
</asp:GridView>
设置后台分页事件PageIndexChanging:
protected void gvProject_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//gvProject.PageIndex = e.NewPageIndex;
//this.BindProjectList();
GridView theGrid = sender as GridView; // refer to the GridView
int newPageIndex = 0;
if (-2 == e.NewPageIndex)
{ // when click the "GO" Button
TextBox txtNewPageIndex = null;
//GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
//updated at 2006年月日:15:33
if (null != pagerRow)
{
txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox; // refer to the TextBox with the NewPageIndex value
}
if (null != txtNewPageIndex)
{
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
}
}
else
{ // when click the first, last, previous and next Button
newPageIndex = e.NewPageIndex;
}
// check to prevent form the NewPageIndex out of the range
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
// specify the NewPageIndex
theGrid.PageIndex = newPageIndex;
// rebind the control
// in this case of retrieving the data using the xxxDataSoucr control,
// just do nothing, because the asp.net engine binds the data automatically
this.BindProjectList();//数据绑定的方法
} 1 <asp:GridView ID="gvProject" runat="server" BorderColor="Gray" Height="20px" Width="98%" AllowPaging="True" AutoGenerateColumns="False" EmptyDataText="没有符合查询条件的数据!" OnDataBound="gvProject_DataBound" OnPageIndexChanging="gvProject_PageIndexChanging" OnRowDataBound="gvProject_RowDataBound" OnRowDeleting="gvProject_RowDeleting">
2 <EditRowStyle BackColor="SeaGreen" />
3 <SelectedRowStyle BorderColor="SeaGreen" />
4 <HeaderStyle BackColor="#718BD6" Font-Size="Small" ForeColor="White" Height="20px" />
5 <PagerSettings Mode="NumericFirstLast" />
6 <RowStyle Height="16px" />
7 <AlternatingRowStyle BackColor="#EDF5FF" />
8 <Columns>
9 <asp:BoundField DataField="ProjectID" HeaderText="项目ID" Visible="False" />
10 <asp:BoundField DataField="ProjectName" HeaderText="项目名称" />
11 <asp:BoundField DataField="ProjectStatus" HeaderText="项目状态" />
12 <asp:BoundField DataField="ssjd" HeaderText="实施进度" >
13 <ItemStyle ForeColor="Red" HorizontalAlign="Center" />
14 </asp:BoundField>
15 <asp:BoundField DataField="ssBeginDate" HeaderText="实施开始时间" DataFormatString="{0:d}" HtmlEncode="False" />
16 <asp:BoundField DataField="ssEndDate" HeaderText="要求结束时间" DataFormatString="{0:d}" HtmlEncode="False" />
17 <asp:BoundField DataField="UserName" HeaderText="项目负责人" />
18 <asp:HyperLinkField DataNavigateUrlFields="ProjectID" DataNavigateUrlFormatString="ProjectInfo.aspx?ProjectID={0}"
19 HeaderText="查看" Text="详细信息" />
20 <asp:TemplateField HeaderText="操作">
21 <ItemTemplate>
22 <asp:ImageButton ID="lbtnDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/drop.gif" />
23 </ItemTemplate>
24 </asp:TemplateField>
25 </Columns>
26 <EmptyDataRowStyle ForeColor="Red" />
27
28 <PagerTemplate>
29 当前第:
30 <asp:Label ID="LabelCurrentPage" runat="server"
31 Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
32 页/共:
33 <asp:Label ID="LabelPageCount" runat="server"
34 Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label> 页
35
36 <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
37 Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton>
38
39 <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"
40 Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton>
41
42 <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
43 Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton>
44
45 <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
46 Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton>
47 转到第
48 <asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
49 <asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" />
50 </PagerTemplate>
51
52 </asp:GridView>