[英]How can I render databound values inside of a custom control?
I'm placing a custom control inside of an existing repeater template. 我将自定义控件放在现有的中继器模板中。
<ItemTemplate>
<custom:MyControl>
<something>
<%# Eval("Firstname") %>
</something>
</custom:MyControl>
</ItemTemplate>
Is there any way that I can somehow get the value of <%# Eval("Firstname") %>
without adding it as a property to my custom control? 有什么方法可以以某种方式获得
<%# Eval("Firstname") %>
而不将其作为属性添加到我的自定义控件中?
<custom:MyControl Firstname='<%# Eval("Firstname") %>' />
I have a bigger need, as in I want to define an entire html structured inner property inside of <something>
and it will be a disaster to do this inside of a property. 我有一个更大的需求,因为我想在
<something>
内定义整个html结构化的内部属性,而在属性内进行此操作将是一个灾难。
So, my repeater is databound, and apparently the control I'm placing within it is not databound, yet the values can be passed through via the property tags... 因此,我的中继器是数据绑定的,显然我放置在其中的控件不是数据绑定的,但是值可以通过属性标签传递...
I found another answer stating it wasn't possible, but it's also 5 year old question. 我发现另一个答案表明这是不可能的,但这也是5年的历史了。 Eval inside an ASP.net repeater doesn't work inside another control
ASP.net中继器中的Eval在另一个控件中不起作用
Grasping at straws, hoping for a solution. 抓住稻草,希望找到解决方案。
You have to create a templated User Control with properties having TemplateContainer
attribute. 您必须使用属性具有
TemplateContainer
属性来创建模板化的用户控件。 See following article for an example. 有关示例,请参见以下文章。
I was able to figure this out, and I'd say the only reason I didn't figure it out sooner is due to testing every combination of methods, but missing a vital element in each one. 我能够弄清楚这一点,我想说我没有更早弄清这一点的唯一原因是由于测试了每种方法的组合,但是却错过了每种方法的重要组成部分。
<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
Maybe there's a cleaner way, but this appears to be working, and my markup now looks like this: 也许有一种更清洁的方法,但这似乎可行,我的标记现在看起来像这样:
<custom:MyControl>
Inner text, with or without <%#Eval("...")%>
</custom:MyControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.