简体   繁体   English

如何在自定义控件中呈现数据绑定值?

[英]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. 有关示例,请参见以下文章。

Creating a Templated User Control 创建模板用户控件

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.

相关问题 如何捕获由数据绑定控件触发的异常? - How can I catch an Exception triggered by databound Control? 使用ItemTemplate(中继器)内部的自定义控件,如何访问中继器项目中的值? - With a custom control that's inside of an ItemTemplate (Repeater), how can I access values from the repeater item? 如何将数据绑定的XAML控件呈现到本机XAML绘图 - How to render a databound XAML control to a native XAML drawing 您可以在数据绑定服务器控件内调用PadLeft吗? - Can you call PadLeft inside a databound server control? 如何使数据绑定控件刷新通过反射更改的属性值? - How can I make a databound control refresh a property value that was changed by reflection? 如何格式化数据绑定文本框的百分比? - How can I format a databound textbox as a percentage? 如何根据ASP.NET中该行的值数据绑定在我的转发器中设置表格行颜色? - How can I set a table row color in my repeater based on the values databound to that row in ASP.NET? 如何创建尽可能多的相同数据绑定组合框,但从同一个字段名中提取不同的值? - How can I create as many of the same databound comboboxes but pull different values from the same fieldname? WPF折线数据绑定到带有MVVM的ObservableCollection自定义控件 - WPF Polyline databound to ObservableCollection Custom Control with MVVM 使Eval在数据绑定的模板化自定义控件中工作 - Getting Eval to work in a databound, templated custom control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM