利用 SqlDataSource进行自定义分页

来源:互联网 发布:熊片数据库 迅雷下载 编辑:程序博客网 时间:2024/03/29 01:01

<table width="100%">
                    <tr>
                        <td style="text-align: center">
                            第<asp:Label   ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>页
                            /共<asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount  %>' />页
                            <% //首页/上一页 %>
                            <% if (this.GVDept.PageIndex <= 0){ %>
                                <asp:LinkButton ID="LinkButton1" runat="server" Enabled="false" Text="首页" />
                                <asp:LinkButton ID="LinkButton2" runat="server" Enabled="false" Text="上一页" />
                                <% }else { %>
                                <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandArgument="First" CommandName="Page" Text="首页" />
                                <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False" CommandArgument="Prev" CommandName="Page" Text="上一页" />
                                <% } %>
                                               
                                <% //下一页/尾页 %>
                                <% if (this.GVDept.PageIndex >= GVDept.PageCount - 1){ %>
                                <asp:LinkButton ID="LinkButton5" runat="server" Enabled="false" Text="下一页" />
                                <asp:LinkButton ID="LinkButton6" runat="server" Enabled="false" Text="尾页" />
                                <% } else { %>
                                <asp:LinkButton ID="LinkButton7" runat="server" CausesValidation="False" CommandArgument="Next" CommandName="Page" Text="下一页" />
                                <asp:LinkButton ID="LinkButton8" runat="server" CausesValidation="False" CommandArgument="Last" CommandName="Page" Text="尾页" />
                                <% } %>
                        </td>
                    </tr>
                </table>       

 

/// <summary>
    /// 点击页码
    /// </summary>
    private void pageChanging(GridView gv, int pn, int pc)
    {
        int newPageIndex = pn;
        newPageIndex = newPageIndex >= pc ? pc - 1 : newPageIndex;
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;

        gv.PageIndex = newPageIndex;
        //判断并取得当前要显示的页号
        string pageno = (string)Request.QueryString["pageno"];
        string pagecount = (string)Request.QueryString["pagecount"];
        SqlConnection pageChageConn = DeptConnection.getConnection();

        SqlCommand pageChageCmd = new SqlCommand("bmPageChage", pageChageConn);
        pageChageCmd.CommandType = CommandType.StoredProcedure;

        if (!string.IsNullOrEmpty(pageno) && !string.IsNullOrEmpty(pagecount))
        {
            pageChageCmd.Parameters.Add("@StartRowIndex", SqlDbType.Int, 4);
            pageChageCmd.Parameters["@StartRowIndex"].Value = (Convert.ToInt32(pageno) - 1) * GVDept.PageSize;

            pageChageCmd.Parameters.Add("@PageSize", SqlDbType.Int, 4);
            pageChageCmd.Parameters["@PageSize"].Value = GVDept.PageSize;
        }

        try
        {
            pageChageConn.Open();
            pageChageCmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(pageChageCmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "Dept");
            //绑定gridview
            GVDept.DataSource = ds.Tables["Dept"].DefaultView;
            GVDept.DataBind();
        }
        catch
        {
        }
        finally
        {
            pageChageCmd.Dispose();
            pageChageConn.Close();
        }
    }

 

 

    protected void GVDept_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //点击分页按钮的事件
        //点击分页按钮的事件
        GridView theGrid = sender as GridView;
        pageChanging(theGrid, e.NewPageIndex, theGrid.PageCount);
        GVDept.PageIndex = e.NewPageIndex;
        gvListBind();
    }

原创粉丝点击