如何创建嵌套LinkButtons内的中继? [英] How To Create A Nested LinkButtons Inside A Repeater?
问题描述
我需要建立一个asp.net页面,看起来像一个树状嵌套LinkButton,但也都是linkbuttons。示例如下所示:
I need to create a nested linkbuttons in a asp.net page that looks like a treeview, but all are linkbuttons. Example is shown below:
ParentLinkButton1
ChildLinkButton1
ChildLinkButton2
ChildLinkButton3
ParentLinkButton2
ChildLinkButton1
ChildLinkButton2
ParentLinkButton3
ParentLinkButton4
ChildLinkButton1
我真的不知道如何做到这一点。根据我的研究这可以使用重复控制来完成,但我不知道该怎么做......如果请你能教我一步一步...
I really don't know how to do this. Based on my research this can be done using repeated control but I don't know how to do that... Please if you can teach me step by step...
在此先感谢!
推荐答案
下面的示例使用一个ListView,而不是一个中继器。列表视图是伟大的,因为他们会给你在一个转发器更大的灵活性。此外,你可以在样品code以下看,结合嵌套/ ListView的孩子都可以做声明的没有任何code-背后的
The following example uses a ListView instead of a Repeater. ListViews are great because they'll give you much more flexibility over a Repeater. Moreover, as you can see in the sample code below, binding the nested/child ListView can all be done declaratively without any code-behind.
什么以下code会产生示例
ASPX
<asp:ListView runat="server" ID="lvw">
<LayoutTemplate>
<ul>
<li id="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:LinkButton runat="server" CommandArgument='<%# Eval("Name")%>'><%# Eval("Name")%></asp:LinkButton>
<asp:ListView runat="server" ID="lvw2" DataSource='<%# Eval("Children")%>'>
<LayoutTemplate>
<ul>
<li id="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li><asp:LinkButton runat="server" CommandArgument='<%# Eval("Name")%>'><%# Eval("Name")%></asp:LinkButton></li>
</ItemTemplate>
</asp:ListView>
</li>
</ItemTemplate>
</asp:ListView>
C#
lvw.DataSource = personList;
lvw.DataBind();
正如你所看到的,在C#code,我创建了如下人的名单。每个人对象具有子Person对象的列表。通过这种方式创建对象,绑定ListView控件真的是那样简单,因为我已经证明。使用下面的Person对象运行快速出样,所以你可以看到自己。
As you can see, in the C# code, I've created a list of "Person" as follows. Each Person object has a list of child Person objects. By creating your objects in this manner, binding the ListView is really as simple as I've shown. Use the Person object below to run a quick sample so you can see for yourself.
Person对象
public class Person
{
public string name { get; set; }
public List<Person> Children { get; set; }
}
有关你的测试,你可以创建一个Page_Load方法如下:
For your test, you can create a Page_Load method as follows:
protected void Page_Load(object sender, EventArgs e)
{
List<Person> personList = new List<Person>();
Person person1 = new Person() { name = "Child 1" };
Person person2 = new Person() { name = "Child 2" };
List<Person> childPersonList1 = new List<Person>();
childPersonList1.Add(person1);
childPersonList1.Add(person2);
Person person3 = new Person() { name = "Person 1" };
person3.Children = childPersonList1;
personList.Add(person3);
Person person4 = new Person() { name = "Child 3" };
Person person5 = new Person() { name = "Child 4" };
List<Person> childPersonList2 = new List<Person>();
childPersonList2.Add(person4);
childPersonList2.Add(person5);
Person person6 = new Person() { name = "Person 2" };
person6.Children = childPersonList2;
personList.Add(person6);
Person person7 = new Person() { name = "Child 5" };
Person person8 = new Person() { name = "Child 6" };
List<Person> childPersonList3 = new List<Person>();
childPersonList3.Add(person7);
childPersonList3.Add(person8);
Person person9 = new Person() { name = "Person 3" };
person9.Children = childPersonList3;
personList.Add(person9);
lvw.DataSource = personList;
lvw.DataBind();
}
请参阅下面的StackOverflow问题更多地了解一个中继器和一个ListView之间的差异:<一href=\"http://stackoverflow.com/questions/139207/repeater-listview-datalist-datagrid-gridview-which-to-choose\">Repeater, ListView控件,DataList控件,DataGrid中,GridView的...选择哪一个?
See the following StackOverflow question to learn more about the differences between a Repeater and a ListView: Repeater, ListView, DataList, DataGrid, GridView ... Which to choose?
这篇关于如何创建嵌套LinkButtons内的中继?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!