Code 1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %> 2 3<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 4 Namespace="System.Web.UI" TagPrefix="asp" %> 5<%@ Import Namespace="System.Data" %> 6<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 7 8<html xmlns="http://www.w3.org/1999/xhtml" > 9<head runat="server">10 <title>无标题页</title>11</head>12<body>13 <form id="form1" runat="server">14 <asp:ScriptManager ID="ScriptManager1" runat="server">15 </asp:ScriptManager>16 <asp:UpdatePanel ID="UpdatePanel1" runat="server">17 <ContentTemplate>18 <asp:Repeater ID="Repeater1" runat="server">19 <HeaderTemplate>20 <table>21 <tr>22 <td>编号</td>23 <td>姓名</td>24 <td>时间</td>25 <td>删除</td>26 </tr>27 </HeaderTemplate>28 <ItemTemplate>29 <tr>30 <td><%#((DataRowView)Container.DataItem)["id"] %></td>31 <td><%#((DataRowView)Container.DataItem)["name"] %></td>32 <td><%#DataBinder.Eval(Container.DataItem, "hiredate","{0:yyyy年MM月dd日}")%></td>33 <td>34 <asp:Button ID="BtnDel" runat="server" Text="删除" OnCommand="BtnDel_Click" CommandName=<%#((DataRowView)Container.DataItem)["id"] %>></asp:Button></td>35 </tr>36 </ItemTemplate>37 <FooterTemplate>38 </table>39 </FooterTemplate>40 </asp:Repeater>41 当前页:<asp:Label ID="currentpage" runat="server"></asp:Label>42 <asp:Button ID="up" runat="server" Text="上一页" OnClick="up_Click" />43 <asp:Button ID="down" runat="server" Text="下一页" OnClick="down_Click" />44 </ContentTemplate>45 </asp:UpdatePanel>46 </form>47</body>48</html>49
上面的代码很简单,作下简单说明: Container.DataItem相当于某个表或视图中的一行(Object类型),在得到该行的某个字段值时首先应该转换成DataRowView(DataRowView 对象将值公开为对象数组,这些数组按基础表中列的名称或序号引用来编制索引),引用DataRowView要用到System.Data命名空间,DataBinder.Eval()是另外一种绑定数据的方式,在这里是为了格式化日期(去除数据库中的时分秒,只显示年月日)。 关于数据绑定可以看李涛的博客 ,这里面讲的很详细。 然后来看后台代码: Code 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 index : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //页面加载时当前页是第一页 currentpage.Text = "1"; //绑定数据 RepeatData(); } } public void RepeatData() { //连接数据库 string connString = "server=.\\sql2005;uid=sa;pwd=sa2005;database=test"; SqlConnection conneciotn = new SqlConnection(connString); //填充数据 SqlDataAdapter sda = new SqlDataAdapter("select * from infos",conneciotn); DataSet ds = new DataSet(); sda.Fill(ds); //运用分页控件 PagedDataSource pds = new PagedDataSource(); //给分页控件数据源 pds.DataSource = ds.Tables[0].DefaultView; //允许分页 pds.AllowPaging = true; //每页显示5条记录 pds.PageSize = 5; //分页控件的分页索引 pds.CurrentPageIndex = Convert.ToInt32(currentpage.Text)-1; this.up.Enabled = true; this.down.Enabled = true; //首页,上一页按钮变灰 if(pds.IsFirstPage) { this.up.Enabled = false; } //尾页,下一页按钮变灰 if(pds.IsLastPage) { this.down.Enabled = false; } //最后未Repeater控件绑定数据源 this.Repeater1.DataSource = pds; this.Repeater1.DataBind(); } //点击上一页按钮的动作 protected void up_Click(object sender, EventArgs e) { this.currentpage.Text = Convert.ToString(Convert.ToInt32(this.currentpage.Text) - 1); RepeatData(); } //点击下一页按钮的动作 protected void down_Click(object sender, EventArgs e) { this.currentpage.Text = Convert.ToString(Convert.ToInt32(this.currentpage.Text) + 1); RepeatData(); } //删除按钮,删除该行,该事件和前台的OnCommand对应 protected void BtnDel_Click(object sender, CommandEventArgs e) { //e.Command获取当前这一行数据的id,id在表中为主键 int id = Convert.ToInt32(e.CommandName); string connString = "server=.\\sql2005;uid=sa;pwd=sa2005;database=test"; SqlConnection conneciotn = new SqlConnection(connString); string sql = "delete from infos where id = " + id; SqlCommand sqlcmd = new SqlCommand(sql, conneciotn); sqlcmd.Connection.Open(); int result = sqlcmd.ExecuteNonQuery(); RepeatData(); }}
同样要实现行的修改,类似于行的删除。Repeater控件需要我们记的东西不多,用起来也极为方便。