【转】关于Repeater 嵌套梆定不明之处

【转自     http://www.cnblogs.com/caodaiming/archive/2008/12/18/1357660.html】

 

今天在网上看到一段代码,是关于Repeater 嵌套的问题,有很多的不明白的地方,希望大家能帮我把这个迷解了;

先看运行的效果:

XML文件:

 

<?xml version="1.0" encoding="utf-8" ?>
<books>
  <book type="计算机">
    <context>C语言</context>
    <context>C#语言</context>
    <context>C++语言</context>
  </book>
  <book type="小说">
    <context>爱情小说</context>
    <context>知音小说</context>
    <context>军事动态</context>
  </book>
</books>

 

 后台代码:

 

protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("Test.xml"));
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
    }
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater p = (Repeater)e.Item.FindControl("Repeater2");
            DataRowView drv = (DataRowView)e.Item.DataItem;
            p.DataSource = drv.CreateChildView("book_context");
            p.DataBind();
        }
    }

 前台代码:

 

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
            <HeaderTemplate>
                Repeater Test</HeaderTemplate>
            <FooterTemplate>
            </FooterTemplate>
            <ItemTemplate>
                <ul>
                    <%#Eval("type") %>
                    <br />
                </ul>
                <asp:Repeater ID="Repeater2" runat="server">
                    <ItemTemplate>
                        <%#Eval("context_Text")%><br />
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
</asp:Repeater>

 不明的地方是:

1、 p.DataSource = drv.CreateChildView("book_context"); 中的“book_context”为什么是这样的参数,修改成其它的参数报错:

去MSDN中查CreateChildView方法解释如下:

如果 DataSet 中的表之间存在关系,则可以使用 DataRowView 的 CreateChildView 方法为父表中的行创建一个 DataView,包含相关子表中的行;具体也没有说明什么,它只是说父表与子表的关系;

2、前面页面代码

 

<ItemTemplate>
  <%#Eval("context_Text")%><br />
</ItemTemplate>

 梆定XML中的context的时候只能是context_Text字段;修改成context是报错;但是按照我们一般性的理解,应该是梆定XML中的字段啊!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值