[英]How can I render databound values inside of a custom control?
我将自定义控件放在现有的中继器模板中。
<ItemTemplate>
<custom:MyControl>
<something>
<%# Eval("Firstname") %>
</something>
</custom:MyControl>
</ItemTemplate>
有什么方法可以以某种方式获得<%# Eval("Firstname") %>
而不将其作为属性添加到我的自定义控件中?
<custom:MyControl Firstname='<%# Eval("Firstname") %>' />
我有一个更大的需求,因为我想在<something>
内定义整个html结构化的内部属性,而在属性内进行此操作将是一个灾难。
因此,我的中继器是数据绑定的,显然我放置在其中的控件不是数据绑定的,但是值可以通过属性标签传递...
我发现另一个答案表明这是不可能的,但这也是5年的历史了。 ASP.net中继器中的Eval在另一个控件中不起作用
抓住稻草,希望找到解决方案。
您必须使用属性具有TemplateContainer
属性来创建模板化的用户控件。 有关示例,请参见以下文章。
我能够弄清楚这一点,我想说我没有更早弄清这一点的唯一原因是由于测试了每种方法的组合,但是却错过了每种方法的重要组成部分。
<ParseChildren(False)>
Public Class Something
Inherits Button ' Just picked something
Public Property InnerStuff() AS String
Protected Overrides Sub OnLoad(e As EventArgs)
EnsureChildControls()
If HasControls() Then
' Interesting, if Eval() has taken place, a
' DataBoundLiteralControl is created as a child
' control.
If TypeOf Controls(0) Is DataBoundLiteralControl Then
Dim instance As DataBoundLiteralControl = CType(Controls(0), DataBoundLiteralControl)
' I can push the text into a property
InnerStuff = instance.Text
' If no databinding occurred, it creates LiteralControl
ElseIf TypeOf Controls(0) Is LiteralControl Then
Dim instance As LiteralControl = CType(Controls(0), LiteralControl)
InnerStuff = instance.Text
End If
MyBase.OnLoad(e)
End Sub
End Class
也许有一种更清洁的方法,但这似乎可行,我的标记现在看起来像这样:
<custom:MyControl>
Inner text, with or without <%#Eval("...")%>
</custom:MyControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.