![](/img/trans.png)
[英]How to access ItemTemplate control from ItemCommand event using Repeater
[英]With a custom control that's inside of an ItemTemplate (Repeater), how can I access values from the repeater item?
我有一个控件,一个自定义按钮。 此按钮位于<ItemTemplate>
内部。 我需要从其父<ItemTemplate>
访问数据绑定值。
例如,除了通过属性标记公开值外, <%#Eval("Firstname")%>
可以在<ItemTemplate>
内完成,但不能在控件本身内完成。
注意 :我知道如何通过属性/属性标签传递这些值。 在这种情况下,我不能这样做。
我一直在玩DataBinder.Eval
但似乎无法访问我所需要的。
尝试示例:
Dim parent As RepeaterItem = CType(Me.NamingContainer, RepeaterItem)
Dim test = DataBinder.Eval(parent.DataItem, "Firstname")
test = DataBinder.Eval(Me.NamingContainer, "DataItem.Firstname")
test = DataBinder.Eval(DirectCast(DirectCast(parent, RepeaterItem), IDataItemContainer).DataItem, "Firstname")
我以前从来没有做过此事,我也想严格地从代码隐藏中做到这一点。 我可以爬上控制树,然后看到“ NamingContainer”实际上是我的重复代码。
我想念什么吗? 欢迎使用C#或VB.net示例。 会转换它。
如您所述,最好的方法是通过属性实际解析值。
而且我不确定您是否正在考虑的回发场景中实际上可以将DataItem作为对象访问。
我认为您实际上必须通过自定义控件中的控件树访问值,这并不可怕。
例如。 假设您的转发器项目的提交按钮“ yourButton”旁边有一个名为“ yourLiteral”的文字。 然后,在代码文件“ yourButton”及其提交事件中,您可以执行以下操作:
(C#)
((Literal) ((RepeaterItem) this.Parent).FindControl("yourLiteral")).Text
获取文本值。 然后替换与您相关的控件类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.