服务端RepeaterTestDemo.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterTestDemo.aspx.cs" Inherits="RepeaterTestDemo" %>

<!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>Repea控件实现分页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
        <table width="100%">
        <tr>
        <td>
        <asp:Label ID="lblid" runat="server"><%#Eval("id") %></asp:Label>
        </td>
        <td>
        <asp:Label ID="Label1" runat="server"><%#Eval("year") %></asp:Label>
        </td>
        <td>
        <asp:Label ID="Label2" runat="server"><%#Eval("classno") %></asp:Label>
        </td>
        <td>
        <asp:Label ID="Label3" runat="server"><%#Eval("classname") %></asp:Label>
        </td>
        <td>
        <asp:Label ID="Label4" runat="server"><%#Eval("modifytime") %></asp:Label>
        </td>
        </tr>
        </table>
        </ItemTemplate>
        </asp:Repeater>
        <br />
        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" Style="position: relative; left: 7px; top: -18px;">首页</asp:LinkButton>
        <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click" Style="position: relative; left: 12px; top: -19px;">上一页</asp:LinkButton>
        <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click" Style="position: relative; left: 18px; top: -18px;">下一页</asp:LinkButton>
        <asp:LinkButton ID="LinkButton4" runat="server" OnClick="LinkButton4_Click" Style="position: relative; left: 27px; top: -19px;">最后一页</asp:LinkButton>
        当前页码:<asp:Label ID="lblCurrentPage" runat="server" ></asp:Label>
        总页码:<asp:Label ID="lblPageCount" runat="server" ></asp:Label></div>
    </form>
</body>
</html>


代码隐藏文件RepeaterTestDemo.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class RepeaterTestDemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            this.lblCurrentPage.Text = "1";
            initDataBinder();
        }
    }

    public void initDataBinder()
    {
        SqlConnection sqlcon = new SqlConnection("server=.;database=Edis;integrated security=sspi");
        using(sqlcon)
        {
            sqlcon.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from tblclass",sqlcon);
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);

                PagedDataSource ps = new PagedDataSource();
               
                int pagecount = Convert.ToInt32(lblCurrentPage.Text);

                ps.DataSource = ds.Tables[0].DefaultView;

                ps.AllowPaging = true;
                ps.PageSize = 4;
                ps.CurrentPageIndex = pagecount - 1;
                this.LinkButton1.Enabled = true;
                this.LinkButton2.Enabled = true;
                this.LinkButton3.Enabled = true;
                this.LinkButton4.Enabled = true;

                if(pagecount==1)
                {
                    this.LinkButton1.Enabled = false;
                    this.LinkButton2.Enabled = false;
                }

                if(pagecount==ps.PageCount)
                {
                    this.LinkButton3.Enabled = false;
                    this.LinkButton4.Enabled = false;
                }

                this.lblPageCount.Text = ps.PageCount.ToString();

                this.Repeater1.DataSource = ps;
                this.Repeater1.DataBind();
            }
            sqlcon.Close();
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        this.lblCurrentPage.Text = "1";
        initDataBinder();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        this.lblCurrentPage.Text = Convert.ToString(Convert.ToInt32(this.lblCurrentPage.Text)-1);
        initDataBinder();
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        this.lblCurrentPage.Text = Convert.ToString(Convert.ToInt32(this.lblCurrentPage.Text)+1);
        initDataBinder();
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        this.lblCurrentPage.Text = this.lblPageCount.Text;
        initDataBinder();
    }
}

Repeater 简单数据绑定

ASPX页面的Repeater控件:
<asp:Repeater ID="IndexTopMain" runat="server">
<HeaderTemplate>
    <!--头-->
    <ul>
</HeaderTemplate>
<ItemTemplate>
    <!--中间循环部分-->
    <li><a href="AritcleDisplay.aspx?id=<%#Eval("id") %>" title="<%#Eval("title") %>" target="_blank"><%#Eval("title") %></a></li>
</ItemTemplate>
<FooterTemplate>
    <!--尾-->
    </ul>
</FooterTemplate>
</asp:Repeater>
CS文件的绑定
string indexTopMainStr = "select top 9 id,title from Table order by ID desc";
SqlDataAdapter rst = new SqlDataAdapter(indexTopMainStr,conn);
conn.Open();
DataSet rdt = new DataSet();
rst.Fill(rdt);
IndexTopMain.DataSource = rdt;//数据绑定
IndexTopMain.DataBind();
conn.Close();

二:
使用:SqlCommand方式
private void bind()
{
SqlConnection conn = db.CreateConnection();
string sql = "select * from gongsia";
SqlCommand cmd = new SqlCommand(sql,conn );
conn.Open();
Repeater1.DataSource = cmd.ExecuteReader();
Repeater1.DataBind();
conn.Close();
}

 

posted on 2010-11-12 20:45  露水丛生  阅读(416)  评论(0编辑  收藏  举报