中继器中的中继器
我的中继器里面有一个中继器。其中父中继器绑定到一个 Datatble
,其中有一列包含 Datatable
。
我想将子中继器绑定到父中继器数据行中的数据表列
这可能吗?我想我可以直接在 aspx
文件中执行此操作,例如:
DataSource="<%# DataBinder.Eval(Container.DataItem, "Products")%>"
但它似乎不起作用。
I have a repeater inside a repeater. Where the parent repeater is bound to a Datatble
which has a column with a Datatable
in it.
I would like to bind the child repeater to the datatable column in the parent repeater's datarow
Is this possible? i was thinking i could do this directly in the aspx
file like:
DataSource="<%# DataBinder.Eval(Container.DataItem, "Products")%>"
but it doesn't seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在父中继器中,将一个方法附加到 OnItemDataBound 事件,并在该方法中找到嵌套的中继器并对其进行数据绑定。
示例 (.aspx):
示例 (.cs):
In the parent repeater, attach a method to the OnItemDataBound event and in the method, find the nested repeater and data bind it.
Example (.aspx):
Example (.cs):
我将向子中继器本身添加一个 DataBinding 事件:
然后只需实现它:
我更喜欢在控件级别而不是
ItemDataBound
级别执行此操作,这样如果您必须删除其中的控件或项目,您的模板中,您不必担心在使用它的父控件中查找代码。一切都由他自己控制。另外,您永远不必执行FindControl
。如果您想将来替换某个控件,只需将其删除,您的代码仍然可以工作,因为它是完全独立的。使用
ItemDataBound
会导致您的代码仍然可以编译,但由于它依赖于子控件,因此在运行时崩溃或出现意外行为。I would add a DataBinding event to the child repeater itself:
Then just implement it:
I prefer to do it at the control level instead of the
ItemDataBound
level so that if you ever have to remove controls or items within your templates you don't have to worry about looking for code in the parent controls that use it. It get's all localize witht he control itself. Plus you never have to do aFindControl
.If you want to replace a control in the future you can just delete it and your code will still work since it is all self contained. Using the
ItemDataBound
would cause your code to still compile but crash or act unexpectedly at runtime because of it's reliance on child controls.其实现方式如下:
因此,如果您知道父表中保存嵌套转发器的子表/数据源的列,则可以将其直接放入 aspx 文件中。
Here is how it's done:
So if you know the column in the parent table that holds the child table/datasource for the nested repeater you can put this directly in the aspx file.
Repeater1 OnItemDataBound 事件,然后是 FindControl Repeater2。 代码隐藏将找不到嵌套的Repeater2!您必须使用FindControl("Repeater2")。
Repeater1 OnItemDataBound event, then FindControl Repeater2. The code-behind will not find the nested Repeater2! You have to use FindControl("Repeater2").
如果我需要这样做,我通常使用父中继器的 ItemDataBound 事件来绑定子中继器。如果 e 是您的 EventArgs 参数,您将可以通过 e.Item.FindControl() 访问子中继器,并通过 e.Item.DataItem 访问数据。
If I need to do that, I usually do it using the ItemDataBound event of the parent repeater to bind the child repeater. If e is your EventArgs parameter, you'll have access to the child repeater via e.Item.FindControl(), and access to the data via e.Item.DataItem.