Repeater控件应用示例

<%# %>符号之间的语句表示数据绑定表达式,Container.DataItem则表示控件当前绑定的项。
还可以使用其他方式进行数据绑定,最常用的是<%#DataBinder.Eval (Container.DataItem, "名称")%>,其中,DataBinder是System.Web.UI命名空间定义的一个类;Eval是一个静态方法,它使用反射来计算数据绑定表达式,传递给DataBinder.Eval的第二个参数指定了当前记录中的字段名;可选的第三个参数是格式化字符串,即固定按照什么格式将这个字段转化成一个字符串。

ContractedBlock.gif ExpandedBlockStart.gif RepeaterControl.aspx
 1None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterControl.aspx.cs"
 2None.gif    Inherits="RepeaterControl" %>
 3None.gif
 4None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5None.gif<html xmlns="http://www.w3.org/1999/xhtml">
 6None.gif<head runat="server">
 7None.gif    <title>Repeater控件应用示例</title>
 8None.gif</head>
 9None.gif<body>
10None.gif    <form id="form1" runat="server">
11None.gif        <div>
12None.gif            &lt;%%&gt;符号之间的语句表示数据绑定表达式,Container.DataItem则表示控件当前绑定的项。<br />
13None.gif            还可以使用其他方式进行数据绑定,最常用的是&lt;%#DataBinder.Eval (Container.DataItem, "名称")%&gt;,其中,DataBinder是System.Web.UI命名空间定义的一个类;Eval是一个静态方法,它使用反射来计算数据绑定表达式,传递给DataBinder.Eval的第二个参数指定了当前记录中的字段名;可选的第三个参数是格式化字符串,即固定按照什么格式将这个字段转化成一个字符串。<br />
14None.gif            <br />
15None.gif            <asp:Repeater ID="MyRepeater" runat="server" OnItemCommand="MyRepeater_ItemCommand">
16None.gif                <HeaderTemplate>
17None.gif                    <table border="1">
18None.gif                        <tr>
19None.gif                            <td>
20None.gif                                作者
21None.gif                            </td>
22None.gif                            <td>
23None.gif                                地址
24None.gif                            </td>
25None.gif                            <td>
26None.gif                                联系
27None.gif                            </td>
28None.gif                        </tr>
29None.gif                </HeaderTemplate>
30None.gif                <ItemTemplate>
31None.gif                    <tr>
32None.gif                        <td><%#DataBinder.Eval(Container.DataItem,"au_lname"%>
33None.gif                        </td>
34None.gif                        <td><%#DataBinder.Eval(Container.DataItem,"address"%>
35None.gif                        </td>
36None.gif                        <td>
37None.gif                            <asp:Button ID="btBuy" runat="server" Text="联系" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"au_lname") %>' />
38None.gif                        </td>
39None.gif                    </tr>
40None.gif                </ItemTemplate>
41None.gif                <FooterTemplate>
42None.gif                    </table>
43None.gif                </FooterTemplate>
44None.gif            </asp:Repeater>
45None.gif            <br />
46None.gif            <asp:Label ID="lbBuy" runat="server"></asp:Label></div>
47None.gif    </form>
48None.gif</body>
49None.gif</html>
50None.gif

ContractedBlock.gif ExpandedBlockStart.gif RepeaterControl.aspx.cs
 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Configuration;
 4None.gifusing System.Collections;
 5None.gifusing System.Web;
 6None.gifusing System.Web.Security;
 7None.gifusing System.Web.UI;
 8None.gifusing System.Web.UI.WebControls;
 9None.gifusing System.Web.UI.WebControls.WebParts;
10None.gifusing System.Web.UI.HtmlControls;
11None.gifusing System.Data.SqlClient;
12None.gif
13None.gifpublic partial class RepeaterControl : System.Web.UI.Page
14ExpandedBlockStart.gifContractedBlock.gifdot.gif{
15InBlock.gif    protected void Page_Load(object sender, EventArgs e)
16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
17InBlock.gif        if (!IsPostBack)
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            SqlConnection myCon = new SqlConnection("server=sony;database=Pubs;User Id=sa;pwd=");
20InBlock.gif            try
21ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
22InBlock.gif                myCon.Open();
23InBlock.gif                SqlCommand myCmd;
24InBlock.gif                myCmd = new SqlCommand("select * from Authors", myCon);
25InBlock.gif                SqlDataReader reader = myCmd.ExecuteReader();
26InBlock.gif                this.MyRepeater.DataSource = reader;
27InBlock.gif                this.MyRepeater.DataBind();
28ExpandedSubBlockEnd.gif            }

29InBlock.gif            finally
30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
31InBlock.gif                myCon.Close();
32ExpandedSubBlockEnd.gif            }

33ExpandedSubBlockEnd.gif        }

34ExpandedSubBlockEnd.gif    }

35InBlock.gif    protected void MyRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
36ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
37InBlock.gif        this.lbBuy.Text=e.CommandArgument.ToString();
38ExpandedSubBlockEnd.gif    }

39ExpandedBlockEnd.gif}

40None.gif

转载于:https://www.cnblogs.com/hide0511/archive/2006/09/03/493480.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值