2010-11-09 37 views
0

假設我有一個asp:Repeater,用於顯示名稱和電話號碼列表。如何訪問asp:Repeater中的當前記錄?

<asp:Repeater runat="server"> 
<ItemTemplate> 
    <p><%# Eval("name") %></p> 
    <p><%# Eval("phoneNumber") %></p> 
</ItemTemplate> 
</asp:Repeater> 

現在,讓我們把它變得更有趣。假設每個名稱有一個或多個電話號碼。我覺得我可以在轉發器中使用轉發器來重複電話號碼。我也可以在我的asp代碼中使用for循環。但是,Eval函數是我知道在運行時訪問當前記錄的唯一方式,Eval僅返回一個字符串,並且只在<%#Eval(「X」)%>中運行。我如何獲得當前記錄成員的電話號碼列表?

回答

1

首先,更改

<%# Eval("name") %> 

<asp:Label runat="server" id="lblName" Text='<%# Eval("name") %>'></asp:Label> 

然後在後臺代碼,你可以使用的FindControl()

foreach (RepeaterItem currentItem in Repeater1.Items) 
{ 
    Label l = (Label)currentItem.FindControl("lblName"); 

    // Now you can access it as you would any other label. 
    Response.Write(l.Text); 
} 
+0

WOW!這是令人費解的!是否真的沒有簡單的方法來引用我的asp代碼中的數據源中的當前記錄? – 2010-11-09 19:56:17

+0

沒有。數據源在下一個回傳中不存在。只是直放站的物品。 – David 2010-11-09 20:49:19

相關問題