如何在转发器内创建嵌套的 LinkButton?
我需要在 asp.net 页面中创建一个嵌套的链接按钮,看起来像树视图,但都是链接按钮。示例如下:
ParentLinkButton1
ChildLinkButton1
ChildLinkButton2
ChildLinkButton3
ParentLinkButton2
ChildLinkButton1
ChildLinkButton2
ParentLinkButton3
ParentLinkButton4
ChildLinkButton1
我真的不知道该怎么做。根据我的研究,这可以通过重复控制来完成,但我不知道该怎么做...请您能一步一步教我...
提前致谢!
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...
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下示例使用 ListView 而不是 Repeater。 ListView 很棒,因为它们比 Repeater 具有更大的灵活性。此外,正如您在下面的示例代码中看到的,绑定嵌套/子 ListView 都可以以声明方式完成,无需任何隐藏代码。
以下代码将生成的示例
ASPX
C#
如您所见,在 C# 代码中,我创建了一个“Person”列表,如下所示。每个 Person 对象都有一个子 Person 对象的列表。通过以这种方式创建对象,绑定 ListView 实际上就像我所展示的一样简单。使用下面的 Person 对象运行一个快速示例,以便您亲自查看。
Person 对象
对于您的测试,您可以创建一个 Page_Load 方法,如下所示:
请参阅以下 StackOverflow 问题以了解有关 Repeater 和 ListView 之间差异的更多信息:Repeater、ListView、DataList、DataGrid、GridView ... 选择哪个?
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.
Example of what the following code will produce
ASPX
C#
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 object
For your test, you can create a Page_Load method as follows:
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?
我建议在中继器内部做一个中继器。
然后在 ItemDataBound 事件中,您可以设置子中继器的数据源并绑定它。
请随时询问任何澄清。我不想为你写下整件事。
编辑:
要绑定父级,您需要使用页面加载或其他页面事件将数据源添加到外部转发器,然后绑定它。
然后您有 2 个选项,即通过使用 eval 访问数据绑定对象或使用父项的 ItemDataBound 事件,按照我在上面的 asp 中显示的方式进行操作。
上面的代码并不完美,但它应该让您了解如何完成其余的工作,
干杯,
I would recommend doing a repeater inside of a repeater.
And then in the ItemDataBound events you can set the data source for the child repeater and bind it.
Feel free to ask for any clarifications. i don't want to write the whole thing for you.
EDIT:
To bind the parent, you would want to use Page Load or some other page event to add the datasource to the outer repeater and then bind it.
And then you have 2 options, wither the way I showed it in the asp above by accessing your databound object using the eval, or by using the parent's ItemDataBound event.
The above code is not perfect, but it should get you a good idea on how to get the rest of the way,
Cheers,