gridview 高级分页

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" Width="449px" OnRowDataBound="GridView1_RowDataBound" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" OnRowCreated="GridView1_RowCreated" AllowPaging="True" OnPageIndexChanged="GridView1_PageIndexChanged" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="pkiib" HeaderText="档号" SortExpression="pkiib" >
<ItemStyle VerticalAlign="Middle" Width="50px" />
</asp:BoundField>
<asp:HyperLinkField DataNavigateUrlFields="pkiib" DataNavigateUrlFormatString="left.aspx?pkiib={0}"
DataTextField="pkiia" NavigateUrl="~/left.aspx" Target="left" HeaderText="报告标题" >
<HeaderStyle VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Left" />
</asp:HyperLinkField>
<asp:BoundField DataField="pkiia" HeaderText="隐藏字段" SortExpression="pkiia" Visible="False" >
<HeaderStyle VerticalAlign="Middle" />
</asp:BoundField>
<asp:HyperLinkField DataNavigateUrlFields="pkiib" DataNavigateUrlFormatString="~/PDFWeb/Default.aspx?pkiib={0}"
HeaderText="电子文档" NavigateUrl="~/PDFWeb/Default.aspx" Target="_blank" Text="图文浏览" >
<ItemStyle Width="80px" />
</asp:HyperLinkField>
</Columns>
<PagerTemplate>
<asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First" CommandName="Page" ForeColor="White">首页</asp:LinkButton>
<asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" CommandArgument="Prev" CommandName="Page" ForeColor="White">上一页</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandArgument="Next" CommandName="Page" ForeColor="White">下一页</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" CommandArgument="Last" CommandName="Page" ForeColor="White">尾页</asp:LinkButton>
第<asp:Label ID="lbpage" runat="server" Text=" <%#((GridView)Container.Parent.Parent).PageIndex + 1 %>"> </asp:Label>页共<asp:Label ID="lbpagecount" runat="server" Text=" <%# ((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>页<asp:TextBox ID="txtPage" runat="server" Text=" <%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>" Width="27px"></asp:TextBox>
<asp:LinkButton ID="btnGO" runat="server" CausesValidation="False" CommandArgument="-1" CommandName="Page" Text="GO" ForeColor="Red"></asp:LinkButton>
</PagerTemplate>


<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<PagerSettings FirstPageText="首页" LastPageText="末页" NextPageText="下一页" PreviousPageText="上一页" />
</asp:GridView>

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Page")
{
if (e.CommandArgument == "-1")
{
try
{
int page = Convert.ToInt32(((TextBox)GridView1.BottomPagerRow.FindControl("txtPage")).Text.ToString()) - 1;
Session["pagesize"] = page;
}
catch (Exception)
{
Session["pagesize"] = 0;
}
}else
Session["pagesize"] = 0;
}

}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int pagesize;
if (Session["pagesize"] == "")
{
if (e.NewPageIndex < 0)
{
pagesize = 0;
}
else
{
if (e.NewPageIndex > GridView1.PageCount - 1)
{
pagesize = GridView1.PageCount - 1;
}
else
{
pagesize = e.NewPageIndex;
}
}
}
else
{
pagesize = Convert.ToInt32(Session["pagesize"].ToString());
Session["pagesize"] = "";

}
//BindGridView(pagesize);//绑定GridView数据
if (pagesize < 0)
pagesize = 0;
GridView1.PageIndex = pagesize;
}

你可能感兴趣的:(GridView)