对 Repeater 进行绑定 使用 DataBinder.Eval(Container.DataItem,"personname")
Posted on 2006-10-25 00:33 孤峰皓月 阅读(857) 评论(0) 编辑 收藏 举报
使用 DataBinder.Eval(Container.DataItem,"personname") 对 Repeater 进行绑定
及介绍Repeater 5个模板的使用
前台页面代码:
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeatrControl.aspx.cs" Inherits="test_DataBind_repeatrControl" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:Repeater ID="Repeater1" runat="server">
13 <AlternatingItemTemplate>
14 <font color="blue">
15 <%# DataBinder.Eval(Container.DataItem,"personname") %>
16 <%# DataBinder.Eval(Container.DataItem,"personsex") %>
17 </font><br />
18 </AlternatingItemTemplate>
19 <FooterTemplate>
20 <h3>页尾</h3>
21 </FooterTemplate>
22 <HeaderTemplate>
23 <h3>页头</h3>
24 </HeaderTemplate>
25 <ItemTemplate>
26 <%# DataBinder.Eval(Container.DataItem,"personname") %>
27 <%# DataBinder.Eval(Container.DataItem,"personsex") %>
28 <br />
29 </ItemTemplate>
30 <SeparatorTemplate>
31 <hr Size="1" Color="blue" />
32 </SeparatorTemplate>
33 </asp:Repeater>
34
35 </div>
36 </form>
37</body>
38</html>
39
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12public partial class test_DataBind_repeatrControl : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if(!this.IsPostBack)
17 {
18 SqlConnection con = DBtest.createCon();
19 SqlDataAdapter sdr = new SqlDataAdapter();
20 sdr.SelectCommand = new SqlCommand("select * from person",con);
21 DataSet ds = new DataSet();
22 sdr.Fill(ds,"emp"); //用数据适配器填充数据集.
23 this.Repeater1.DataSource = ds;
24 this.Repeater1.DataBind();
25
26 }
27 }
28}
29
及介绍Repeater 5个模板的使用
前台页面代码:
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeatrControl.aspx.cs" Inherits="test_DataBind_repeatrControl" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:Repeater ID="Repeater1" runat="server">
13 <AlternatingItemTemplate>
14 <font color="blue">
15 <%# DataBinder.Eval(Container.DataItem,"personname") %>
16 <%# DataBinder.Eval(Container.DataItem,"personsex") %>
17 </font><br />
18 </AlternatingItemTemplate>
19 <FooterTemplate>
20 <h3>页尾</h3>
21 </FooterTemplate>
22 <HeaderTemplate>
23 <h3>页头</h3>
24 </HeaderTemplate>
25 <ItemTemplate>
26 <%# DataBinder.Eval(Container.DataItem,"personname") %>
27 <%# DataBinder.Eval(Container.DataItem,"personsex") %>
28 <br />
29 </ItemTemplate>
30 <SeparatorTemplate>
31 <hr Size="1" Color="blue" />
32 </SeparatorTemplate>
33 </asp:Repeater>
34
35 </div>
36 </form>
37</body>
38</html>
39
后台代码:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12public partial class test_DataBind_repeatrControl : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if(!this.IsPostBack)
17 {
18 SqlConnection con = DBtest.createCon();
19 SqlDataAdapter sdr = new SqlDataAdapter();
20 sdr.SelectCommand = new SqlCommand("select * from person",con);
21 DataSet ds = new DataSet();
22 sdr.Fill(ds,"emp"); //用数据适配器填充数据集.
23 this.Repeater1.DataSource = ds;
24 this.Repeater1.DataBind();
25
26 }
27 }
28}
29