在gridview外部实现分页
以下为代码:
<TR>
<TD> <asp:GridView ID="GridView1" runat="server" CssClass="GridViewStyle" GridLines="None">
<FooterStyle CssClass="GridViewFooterStyle" />
<RowStyle CssClass="GridViewRowStyle" />
<PagerStyle CssClass="GridViewPagerStyle" />
<SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
<EditRowStyle CssClass="GridViewEditRowStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
</asp:GridView>
</TR>
</TD>
<TR>
<TD>
<asp:ImageButton ID="IMLastPage" runat="server" ImageUrl="~/EIPOA/images/last02.gif" />最后一页</td>
跳转到<textboxt></text>页....其他类似
<TD>
<TR>
然后我不想在GridView内部使用分页,需要在外部用imagebutton实现"下一页","上一页","跳转到第几页","共多少页","当前为第几页",请问怎样解决,有源码者高分送出,谢谢
[解决办法]
private static string ConStr = ConfigurationManager.ConnectionStrings["ConnectionNorthwind"].ConnectionString; double _totalPages; Int32 _currentPageNumber = 1; //以下两种的存储过程数据表中的数据都是万级别的,各种方法的效率差别不是很大 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindDataReturn(_currentPageNumber); // BindData(_currentPageNumber); } } //调用采用临时表分页的存储过程 private void BindData(int pageIndex) { using (SqlConnection Con = new SqlConnection(ConStr)) { using (SqlCommand Cmd = new SqlCommand("Get_Customers_By_Page", Con)) { Cmd.CommandType = CommandType.StoredProcedure; Cmd.Parameters.AddWithValue("@CurrentPage", pageIndex); Cmd.Parameters.AddWithValue("@PageSize", gvCustomer.PageSize); SqlParameter ParmTotal = new SqlParameter("@TotalRecords", SqlDbType.Int); //获取存储过程中的输出参数 ParmTotal.Direction = ParameterDirection.Output; Cmd.Parameters.Add(ParmTotal); //Cmd.Parameters.Add(new SqlParameter("@TotalRecords",SqlDbType.Int)).Direction = ParameterDirection.Output; Con.Open(); gvCustomer.DataSource = Cmd.ExecuteReader(CommandBehavior.CloseConnection); gvCustomer.DataBind(); CurrentPage.Text = _currentPageNumber.ToString(); if (!Page.IsPostBack) { //必须关闭DR才能正确的获取输出参数的值 int Total = ((Int32)Cmd.Parameters["@TotalRecords"].Value); _totalPages = Total / ((int)gvCustomer.PageSize); TotalPages.Text = (System.Math.Ceiling(_totalPages)).ToString(); } if (_currentPageNumber == 1) { PreviousPage.Enabled = false; if (_totalPages > 1) { NextPage.Enabled = true; } else { NextPage.Enabled = false; } } else { PreviousPage.Enabled = true; if (_currentPageNumber == _totalPages) { NextPage.Enabled = false; } else { NextPage.Enabled = true; } } } } } //调用采用select top分页存储过程 private void BindDataReturn(Int32 pageIndex) { using (SqlConnection Con = new SqlConnection(ConStr)) { using (SqlCommand Cmd = new SqlCommand("PagingSelectTop", Con)) { Cmd.CommandType = CommandType.StoredProcedure; Cmd.Parameters.AddWithValue("@PageCount", pageIndex); Cmd.Parameters.AddWithValue("@RowCount", gvCustomer.PageSize); SqlParameter ParmRowCount = new SqlParameter("@TotalRecord",SqlDbType.Int); //获取存储过程中的返回值 ParmRowCount.Direction = ParameterDirection.ReturnValue; Cmd.Parameters.Add(ParmRowCount); Con.Open(); using (SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection)) { gvCustomer.DataSource = Dr; gvCustomer.DataBind(); CurrentPage.Text = _currentPageNumber.ToString(); } int Total; if (!Page.IsPostBack) { Total = (Int32)Cmd.Parameters["@TotalRecord"].Value; _totalPages = Total / ((int)gvCustomer.PageSize); TotalPages.Text = (System.Math.Ceiling(_totalPages)).ToString(); } } } } //分页导航 protected void NavigationLink_Click(Object sender, CommandEventArgs e) { switch (e.CommandName) { case "First": _currentPageNumber = 1; break; case "Last": _currentPageNumber = Int32.Parse(TotalPages.Text); break; case "Next": _currentPageNumber = (int)(Int32.Parse(CurrentPage.Text) + 1); break; case "Prev": _currentPageNumber = Int32.Parse(CurrentPage.Text) - 1; break; } //BindData(_currentPageNumber); BindDataReturn(_currentPageNumber); }
[解决办法]
我刚写了个分页控件 正符合你的要求 只两百多行代码 有说明和下载
http://blog.csdn.net/WO_YOU_XIE_SHANG_XIN/archive/2008/05/05/2393801.aspx