首先给GridView,DataList和Repeater这3大数据绑定控件作下比较。三者都能够绑定数据源,而不用去手动构造循环结构,GridView会自动生成许多布局控制,而DataList控件灵活性很好,它使用<table>进行数据展示(自动生成),但是现在的页面布局都倾向于用DIV来布局,Repeater则不会自动生成任何标签,它只用于绑定数据,我们可以用我们想要的方式去为它布局。显而易见,GridView因为生成很多标签,效率最差;DataList仅生成少量标签,效率远远高于GridView;Repeater不会生成任何标签,效率就属它最高了。
说了这么多,还是来个简单点的例子吧。
首先前台代码:
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
上面的代码很简单,作下简单说明:
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()是另外一种绑定数据的方式,在这里是为了格式化日期(去除数据库中的时分秒,只显示年月日)。
关于数据绑定可以看李涛的博客 http://www.cnblogs.com/terryli/archive/2008/03/25/1120482.html,这里面讲的很详细。
然后来看后台代码:
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控件需要我们记的东西不多,用起来也极为方便。
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();
}
}