<%# %>符号之间的语句表示数据绑定表达式,Container.DataItem则表示控件当前绑定的项。
还可以使用其他方式进行数据绑定,最常用的是<%#DataBinder.Eval (Container.DataItem, "名称")%>,其中,DataBinder是System.Web.UI命名空间定义的一个类;Eval是一个静态方法,它使用反射来计算数据绑定表达式,传递给DataBinder.Eval的第二个参数指定了当前记录中的字段名;可选的第三个参数是格式化字符串,即固定按照什么格式将这个字段转化成一个字符串。
RepeaterControl.aspx
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterControl.aspx.cs"
2
Inherits="RepeaterControl" %>
3![None.gif](/Images/OutliningIndicators/None.gif)
4
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
6
<head runat="server">
7
<title>Repeater控件应用示例</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
<%# %>符号之间的语句表示数据绑定表达式,Container.DataItem则表示控件当前绑定的项。<br />
13
还可以使用其他方式进行数据绑定,最常用的是<%#DataBinder.Eval (Container.DataItem, "名称")%>,其中,DataBinder是System.Web.UI命名空间定义的一个类;Eval是一个静态方法,它使用反射来计算数据绑定表达式,传递给DataBinder.Eval的第二个参数指定了当前记录中的字段名;可选的第三个参数是格式化字符串,即固定按照什么格式将这个字段转化成一个字符串。<br />
14
<br />
15
<asp:Repeater ID="MyRepeater" runat="server" OnItemCommand="MyRepeater_ItemCommand">
16
<HeaderTemplate>
17
<table border="1">
18
<tr>
19
<td>
20
作者
21
</td>
22
<td>
23
地址
24
</td>
25
<td>
26
联系
27
</td>
28
</tr>
29
</HeaderTemplate>
30
<ItemTemplate>
31
<tr>
32
<td><%#DataBinder.Eval(Container.DataItem,"au_lname") %>
33
</td>
34
<td><%#DataBinder.Eval(Container.DataItem,"address") %>
35
</td>
36
<td>
37
<asp:Button ID="btBuy" runat="server" Text="联系" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"au_lname") %>' />
38
</td>
39
</tr>
40
</ItemTemplate>
41
<FooterTemplate>
42
</table>
43
</FooterTemplate>
44
</asp:Repeater>
45
<br />
46
<asp:Label ID="lbBuy" runat="server"></asp:Label></div>
47
</form>
48
</body>
49
</html>
50
RepeaterControl.aspx.cs
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Data.SqlClient;
12![None.gif](/Images/OutliningIndicators/None.gif)
13
public partial class RepeaterControl : System.Web.UI.Page
14![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
{
15
protected void Page_Load(object sender, EventArgs e)
16![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
if (!IsPostBack)
18![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
SqlConnection myCon = new SqlConnection("server=sony;database=Pubs;User Id=sa;pwd=");
20
try
21![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
myCon.Open();
23
SqlCommand myCmd;
24
myCmd = new SqlCommand("select * from Authors", myCon);
25
SqlDataReader reader = myCmd.ExecuteReader();
26
this.MyRepeater.DataSource = reader;
27
this.MyRepeater.DataBind();
28
}
29
finally
30![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
myCon.Close();
32
}
33
}
34
}
35
protected void MyRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
36![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
37
this.lbBuy.Text=e.CommandArgument.ToString();
38
}
39
}
40
转载于:https://www.cnblogs.com/hide0511/archive/2006/09/03/493480.html