vs2005下使用ASPNetPage分页的例子1

页面设计
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <webdiyer:AspNetPager ID="AspNetPager1" runat="server" PageSize="1" OnPageChanged="AspNetPager1_PageChanged" OnPageChanging="AspNetPager1_PageChanging">
        </webdiyer:AspNetPager>
    </div>
    </form>
</body>
</html>

CodeBehide

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            GetLog();
    }
private void GetLog()
    {

        string strsql;

        //统计总的记录数

        SqlConnection Conn = new SqlConnection();

        Conn.ConnectionString = @"server=localhost\\PMSERVER;uid=;pwd=;database=test";
        try
        {
            MeetWeb.SqlDataBase a = new MeetWeb.SqlDataBase("server=localhost\\PMSERVER;uid=sa;pwd=;database=test");
    

            strsql = " Select count(ID) from test";

            DataSet ds = new DataSet();

            SqlDataAdapter Adp = a.CreateAdapter(strsql);

            Adp.Fill(ds, "test"); this.AspNetPager1.RecordCount = System.Convert.ToInt32(ds.Tables[0].Rows[0][0]);

          
            Response.Write(ds.Tables[0].Rows[0][0]);
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
 BindData();
    }

    private void BindData()
    {

        string strsql;

        int RecordPage = (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize;

       
   

        strsql = String.Format(" Select Top  {0} *  From test Where  Id Not in ( Select Top {1} Id From test  Order By ID Desc) Order By ID Desc", AspNetPager1.PageSize.ToString(), RecordPage.ToString());
        DataSet ds = new DataSet();
        MeetWeb.SqlDataBase a = new MeetWeb.SqlDataBase("server=;uid=;pwd=;database=test");
    
        SqlDataAdapter Adp =a.CreateAdapter(strsql);

        Adp.Fill(ds, "test");

        this.GridView1.DataSource = ds.Tables["test"];

        this.GridView1.DataBind();

        //动态设置用户自定义文本内容

        Adp.Dispose();

        AspNetPager1.CustomInfoHTML= "记录总数:<font color=\"blue\"><b>" + AspNetPager1.RecordCount.ToString() + "</b></font>";

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

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

    }

//注意这里 和vs2003有区别
 protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;

        BindData();

        System.Text.StringBuilder sb = new StringBuilder("<script Language=\"Javascript\"><!--\n");


        sb.Append("var el=document.all;");

        sb.Append(this.GridView1.ClientID);

        sb.Append(".scrollIntoView(true);");

        sb.Append("<");

        sb.Append("/");

        sb.Append("script>");

        if (!ClientScript.IsStartupScriptRegistered("scrollScript"))

            ClientScript.IsStartupScriptRegistered(sb.ToString());
    }

posted @ 2007-03-02 10:04  meetweb  阅读(1688)  评论(1编辑  收藏  举报