0

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?

<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.

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. Eval inside an ASP.net repeater doesn't work inside another control

Grasping at straws, hoping for a solution.

Community
  • 1
  • 1
user1447679
  • 3,076
  • 7
  • 32
  • 69

2 Answers2

0

You have to create a templated User Control with properties having TemplateContainer attribute. See following article for an example.

Creating a Templated User Control

Sam
  • 2,917
  • 1
  • 15
  • 28
  • I could be immediately misunderstanding, but it still looks like the only data-bound available values are going to come from within that templated controls own data source. But I'm going to give this a good read. Thank you for your fast reply. This has become very frustrating. – user1447679 Apr 19 '16 at 02:18
  • The thing is without having properties you can't pass values into a user control. Unless you use session or query string, which are not ideal in this case. If you have lots of properties to bind I'd suggest using this method rather than passing the values through properties. – Sam Apr 19 '16 at 02:25
  • Otherwise, if you have lots of properties to bind how about creating a class with those properties and create just one property of that class within your user control. The data source for your repeater should be a collection of that class. https://derekreynolds.wordpress.com/2010/08/18/how-to-embed-a-usercontrol-in-a-repeater/ – Sam Apr 19 '16 at 02:56
  • Interesting. One issue here is I can only go one way. I can't access the code from the repeater. I can only place a custom control within it. Here's a thought. What if I could implement some type of databound interface on my control, and climb the control tree to get the data from the `` that it's sitting in? If I had some way of creating a list(of string, string) on names and values for dataitems I think that would work. – user1447679 Apr 19 '16 at 03:11
  • You don't need to have access to code behind code if you need to bind data objects. You can do it within the markup. You can use MyProperty='<%# ((MyObjects.SomeBusinessObj)Container.DataItem).CustomProperty %>'. See detailed answer here http://stackoverflow.com/questions/4246725/repeater-control-with-objects – Sam Apr 19 '16 at 04:50
  • By the way, why don't you have access to code behind? – Sam Apr 19 '16 at 04:51
  • I meant I don't have access to the source code of the parent repeater. I created a templated control based on that tutorial, but still no way to grab the values from the parent ``. It drives me crazy that I can pass these values in via property tags, but not use them within the control. So my control isn't databound but the parent obviously is. – user1447679 Apr 19 '16 at 18:25
  • I think I may have figured this out. Will let you know. – user1447679 Apr 19 '16 at 18:55
  • take a look at my answer. So far so good. Would love to hear your thoughts. – user1447679 Apr 19 '16 at 23:13
0

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>
user1447679
  • 3,076
  • 7
  • 32
  • 69