0
Repeater in Repeater是我的問題的答案。直放站內直放站 - 家長ID
但是,我很困惑childRepeater.DataSource = ...;
因爲我需要使用一種方法來獲取數據,並且該方法使用參數parent ID
。我不明白我是如何得到parent ID
的值 - 外部中繼器的ID。
我看了一些其他的例子,但無人接聽的作品......
Repeater in Repeater是我的問題的答案。直放站內直放站 - 家長ID
但是,我很困惑childRepeater.DataSource = ...;
因爲我需要使用一種方法來獲取數據,並且該方法使用參數parent ID
。我不明白我是如何得到parent ID
的值 - 外部中繼器的ID。
我看了一些其他的例子,但無人接聽的作品......
使用參考答案會是這樣的,只是檢查當然是空值。
protected void ItemBound(object sender, RepeaterItemEventArgs args)
{
if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater childRepeater = (Repeater)args.Item.FindControl("ChildRepeater");
Repeater parentRepeater = (Repeater)args.Item.FindControl("ParentRepeater");
childRepeater.DataSource = YourMethod(parentRepeater.ID);
childRepeater.DataBind();
}
}
<asp:Repeater ID="ParentRepeater" runat="server"
OnItemDataBound="ParentRepeater_ItemDataBound">
<ItemTemplate>
<strong>Parent</strong>
<asp:Label runat="server" ID="FirstNameLabel"
Text='<%# Eval("FirstName") %>' />
<asp:Label runat="server" ID="LastNameLabel"
Text='<%# Eval("LastName") %>' />
<br/>
<!-- Repeated data -->
<asp:Repeater ID="ChildRepeater" runat="server">
<ItemTemplate>
<!-- Nested repeated data -->
<strong>Children</strong>
<asp:Label runat="server" ID="FirstNameLabel"
Text='<%# Eval("FirstName") %>' />
<asp:Label runat="server" ID="LastNameLabel"
Text='<%# Eval("LastName") %>' /><br/>
</ItemTemplate>
</asp:Repeater>
<hr/>
</ItemTemplate>
</asp:Repeater>
public class User
{
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<User> Children;
public User()
{
Children = new List<User>();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var users = new List<User>
{
new User
{
UserId = 1,
FirstName = "Jon",
LastName = "Doe",
Children = new List<User> {new User {
UserId = 3, FirstName = "Marry", LastName = "Doe"}}
},
new User
{
UserId = 2,
FirstName = "Eric",
LastName = "Newton",
Children = new List<User> {new User {
UserId = 3, FirstName = "Nick", LastName = "Newton"}}
}
};
ParentRepeater.DataSource = users;
ParentRepeater.DataBind();
}
}
protected void ParentRepeater_ItemDataBound(object sender,
RepeaterItemEventArgs args)
{
if (args.Item.ItemType == ListItemType.Item ||
args.Item.ItemType == ListItemType.AlternatingItem)
{
var user = args.Item.DataItem as User;
var childRepeater = (Repeater) args.Item.FindControl("ChildRepeater");
// You can get parent id like this -
// int parentUserId = user.UserId;
childRepeater.DataSource = user.Children;
childRepeater.DataBind();
}
}
你怎麼能找到** ** ParentRepeater裏面** ** ParentRepeater的*** ItemBound ***? – Win
你是對的,他可以使用然後:childRepeater.Parent.ID – cvetyab
**好猜 - childRepeater.Parent.ID將無法工作。**有多種方式訪問ParentID,如使用HiddenField。 – Win