DataList分页的实现

 

<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" >
        <HeaderTemplate>
         <table>
          <tr>
          <td>编号</td><td>姓名</td><td>年龄</td><td>地址</td>
          </tr>
         </table>
        </HeaderTemplate>
        <ItemTemplate>
        <table>
        <tr>
        <td> <asp:Label ID="idLabel" runat="server" Text='<%# Eval("ID") %>' ></asp:Label></td>
        <td><asp:Label ID="nameLabel" runat="server" Text='<%#Eval("NAME") %>'></asp:Label> </td>
        <td><asp:Label ID="ageLabel" runat="server" Text='<%#Eval("AGE") %>'></asp:Label></td>
        <td><asp:Label ID="matterLabel" runat="server" Text='<%#Eval("ADDRESS") %>'></asp:Label> </td>
        </tr>
        </table>
        </ItemTemplate>
           </asp:DataList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:HoncalTestConnectionString %>"
            SelectCommand="SELECT [ID], [NAME], [AGE], [ADDRESS] FROM [Stu]">
        </asp:SqlDataSource>
        <br />
    </div>
    <div>
        <asp:LinkButton ID="FirstLB" runat="server"  onclick="FirstLB_Click" >第Ì页</asp:LinkButton>
        <asp:LinkButton ID="PreviousLB" runat="server" onclick="PreviousLB_Click">上一页</asp:LinkButton>
        <asp:LinkButton ID="NextLB" runat="server" onclick="NextLB_Click">下一页</asp:LinkButton>
        <asp:LinkButton ID="EndLB" runat="server" onclick="EndLB_Click">最后一页</asp:LinkButton>
    </div>
    <div>
    总<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>页
    当前第<asp:Label ID="Label1" runat="server" Text="1"></asp:Label>页
        <asp:LinkButton ID="JumpLB" runat="server" onclick="JumpLB_Click" >跳到</asp:LinkButton>第
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>页°3
    </div>
    </form>
</body>
============================================================================
后台代码:
public partial class _new : System.Web.UI.Page
    {
        int CurrentIndex;//当前数
        protected void Page_Load(object sender, EventArgs e)
        {
            DataBinds();
        }
        protected void DataBinds()
        {
            SqlConnection cn = new SqlConnection(SqlDataSource1.ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter(SqlDataSource1.SelectCommand, cn);
            // Dataset ds = new Dataset();
            PagedDataSource pds = new PagedDataSource();//初?始º?化¡¥分¤?页°3事º?例¤y
            pds.DataSource = (System.Data.DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            pds.AllowPaging = true;
            pds.PageSize = 5;
            CurrentIndex = int.Parse(this.Label1.Text) - 1;//获?取¨?当Ì¡À前¡ã页°3的Ì?索¡Â引°y
            pds.CurrentPageIndex = CurrentIndex;
            if (CurrentIndex == 0)
            {
                this.PreviousLB.Enabled = false;
                this.FirstLB.Enabled = true;
                this.NextLB.Enabled = true;
                this.EndLB.Enabled = true;
            }
            else if (CurrentIndex == pds.Count - 1)
            {
                this.PreviousLB.Enabled = true;
                this.FirstLB.Enabled = true;
                this.NextLB.Enabled = false;
                this.EndLB.Enabled = false;
            }
            else
            {
                this.PreviousLB.Enabled = true;
                this.FirstLB.Enabled = true;
                this.NextLB.Enabled = true;
                this.EndLB.Enabled = true;
            }
            this.Label2.Text = pds.PageCount.ToString();
            DataList1.DataSource = pds;
            DataList1.DataBind();
        }
        protected void FirstLB_Click(object sender, EventArgs e)//首º¡Á页°3
        {
            this.Label1.Text = "1";//页°3数ºy为a1
            DataBinds();
        }
        protected void PreviousLB_Click(object sender, EventArgs e)
        {
            this.Label1.Text = (int.Parse(Label1.Text) - 1).ToString();//页°3数ºy减?1
            DataBinds();
        }
        protected void NextLB_Click(object sender, EventArgs e)//下?一°?页°3
        {
            this.Label1.Text = (int.Parse(this.Label1.Text) + 1).ToString();//页°3数ºy加¨®1
            DataBinds();
        }
        protected void EndLB_Click(object sender, EventArgs e)//末?页°3
        {
            this.Label1.Text = Label2.Text;//页°3数ºy为a最Á?后¨®一°?页°3
            DataBinds();
        }
        protected void JumpLB_Click(object sender, EventArgs e)
        {
            try
            {
                if (int.Parse(TextBox1.Text) > 0 && int.Parse(TextBox1.Text) <= int.Parse(Label2.Text))
                {
                    this.Label1.Text = TextBox1.Text;
                    DataBinds();
                }
                else
                {
                    Response.Write("<script>alert('请?输º?入¨?有®D效¡ì数ºy字Á?')</script>");
                    TextBox1.Text = null;
                }
            }
            catch
            {
                Response.Write("<script>alert('系¦Ì统ª3出?错䨪')</script>");
                Response.Redirect("~/Default.aspx");
            }
        }
    }
=======================================================================================================================

posted @ 2011-11-06 10:30  honkcal  阅读(1475)  评论(0编辑  收藏  举报