AspNetPager(分页控件)的使用

AspNetPager的使用
AspNetPager的使用(一)

其使用是通过储存过程来实现的
储存过程如下:

CREATE Procedure     Pr_AritleAllInfo
(@PageSize int,
@PageIndex int,
@Docount bit)
As
Set nocount on
If(@docount=1)
Select count(*) from Aritle
Else
Begin
Declare @indextable table(id int identity(1,1) , KeyNo uniqueidentifier)
Declare @PageLowerBound int
Declare @PageUpperBound int
Set @PageLowerBound=(@PageIndex-1)*@PageSize
Set @PageUpperBound=@PageLowerBound+@PageSize
Set RowCount @PageUpperBound
Insert into @indextable(KeyNo) select KeyNo From Aritle order by AddTime desc
Select O.* from Aritle O,@indextable t where O.KeyNo=t.KeyNo
And t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id
End
GO
Default.apx页面:

<center>

        <asp:DataList ID="DataList1" runat="server">

        <HeaderTemplate><table width="760"  border="0" cellspacing="0" cellpadding="0" style="border-bottom:1px dotted #ccc;"  id="table5" ></HeaderTemplate>

        <ItemTemplate >

        <tr>

        <td><%#Eval("title") %></td>

        </tr></ItemTemplate>

        <FooterTemplate ></table></FooterTemplate>

        </asp:DataList>

        <table border="0" cellpadding="0" style="border-collapse: collapse; background-color: #f5f5f5;" width="760" id="table1" class="mu6" height="20">

    <tr>

       <td style="height: 25px">

         <Webdiyer:AspNetPager id="Pager" runat="server" HorizontalAlign="Right"  FirstPageText="<<" LastPageText=">>" PrevPageText="<" NextPageText=">" NumericButtonTextFormatString="-{0}-" Width="760px"

           ShowCustomInfoSection="Left" ShowBoxThreshold="2" PageSize="5" OnPageChanged="pager_PageChanged" InputBoxClass="text2" TextAfterInputBox=""  />

           </td>

    </tr>

</table>

        </center>

AspNetPage使用(二)

Default.aspx.cs

public partial class Page : System.Web.UI.Page

{

    public SqlConnection Conn;

    public SqlCommand Cmd;

    protected void Page_Load(object sender, EventArgs e)

    {

        String ConnString = DataConn.connetionString;

        Conn = new SqlConnection(ConnString);

        if (!IsPostBack)

        {

            FristBind();

        }

    }

    protected void FristBind()

    {

        Cmd = new SqlCommand("Pr_AritleAllInfo", Conn);

        Cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter ParaPageIndex = new SqlParameter("@PageIndex", SqlDbType.Int);

        ParaPageIndex.Value = 1;

        Cmd.Parameters.Add(ParaPageIndex);

        SqlParameter ParaPageSize = new SqlParameter("@PageSize", SqlDbType.Int);

        ParaPageSize.Value = 1;

        Cmd.Parameters.Add(ParaPageSize);

        SqlParameter ParaDocount = new SqlParameter("@Docount", SqlDbType.Bit);

        ParaDocount.Value = true;

        Cmd.Parameters.Add(ParaDocount);

        Conn.Open();

        Pager.RecordCount = (int)Cmd.ExecuteScalar();

        Conn.Close();

        BindDataList();

    }

    protected void BindDataList()

    {

        Cmd = new SqlCommand();

        Cmd.Connection = Conn;

        Cmd.CommandType = CommandType.StoredProcedure;

        Cmd.CommandText = "Pr_AritleAllInfo";

        SqlParameter ParaPageIndex=new SqlParameter ("@PageIndex",SqlDbType .Int );

        ParaPageIndex.Value =Pager .CurrentPageIndex ;

        Cmd .Parameters .Add (ParaPageIndex );

        SqlParameter ParaPageSize = new SqlParameter("@PageSize",SqlDbType .Int);

        ParaPageSize.Value = Pager.PageSize ;

        Cmd.Parameters.Add(ParaPageSize);

        SqlParameter ParaDocount = new SqlParameter("@Docount", SqlDbType.Bit);

        ParaDocount.Value = false;

        Cmd.Parameters.Add(ParaDocount);

        Conn.Open();

        DataList1.DataSource = Cmd.ExecuteReader();

        DataList1.DataBind();

        Conn.Close();

        AddCustomText();

    }

    public void AddCustomText()

    {

        Pager.CustomInfoText = "文章总数:<font color=""blue""><b>" + Pager.RecordCount.ToString() + "</b></font>";

        Pager.CustomInfoText += " 总页数:<font color=""blue""><b>" + Pager.PageCount.ToString() + "</b></font>";

        Pager.CustomInfoText += " 当前页:<font color=""red""><b>" + Pager.CurrentPageIndex.ToString() + "</b></font>";

    }

    protected void pager_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)

    {

        Pager.CurrentPageIndex = e.NewPageIndex;

        BindDataList();

    }

}

    
方法三:
DataSet ds;
SqlDataAdapter dr;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("server=.;uid=sa;database=数据库");
con.Open();
com = new SqlCommand();
com.Connection = con;
com.CommandText = "select count(*) from Employees";
AspNetPager1.AlwaysShow=true;
AspNetPager1.PageSize=15;
AspNetPager1.RecordCount = (int)com.ExecuteScalar();
con.Close();
DataListDataBind();
}
}
private void DataListDataBind()
{
SqlConnection con = new SqlConnection("server=.;uid=sa;database=数据库");
dr = new SqlDataAdapter("select * from Employees", con);
ds = new DataSet();
dr.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "Employees");
DataList1.DataSource = ds.Tables["Employees"];
DataList1.DataBind();

}

protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
DataListDataBind();
}
posted @ 2008-03-27 14:47  supers  阅读(277)  评论(0)    收藏  举报