關於Repeater 嵌套梆定不明之處

來源:互聯網
上載者:User
今天在網上看到一段代碼,是關於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>

後台代碼:

Code
 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方法解釋如下:

如果 中的表之間存在關係,則可以使用 的 方法為父表中的行建立一個,包含相關子表中的行;具體也沒有說明什麼,它只是說父表與子表的關係;

2、前面頁面代碼

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

梆定XML中的context的時候只能是context_Text欄位;修改成context是報錯;但是按照我們一般性的理解,應該是梆定XML中的欄位啊!

請教園子中的朋友給我把心中的這個迷解了;萬分感謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.