[RESOLVED] Create Repeater programmatically
I am trying to create a repeater programmatically and am having trouble figureing out how to put the Eval("column name") code into the template.
I saw some code that said to use Databinder.Eval(Container.DataItem, "columnname"), but when I do Container. DataItem is not available in the object. I tried renaming the container to cont in the instantiate in sub but that didnt make the other container object available.
Here is the original code done the normal way.
Code:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div>
<strong>Q.</strong><a style="margin-left: 10px;" href='javascript:toggleDiv(<%#Eval("ItemID") %>)'><%#Eval("Question")%></a><br />
<div id='<%#Eval("ItemID") %>' class="Answers"><%#Eval("Answer")%></div>
</div>
</ItemTemplate>
</asp:Repeater>
Here is the code I have so far
Code:
Dim faqRepeat As New Repeater
Protected sub page_init(byval...., ...) handles me.init
faqRepeat.ItemTemplate = New MyTemplate
end sub
Public Class MyTemplate
Implements ITemplate
Sub InstantiateIn(ByVal container As Control) _
Implements ITemplate.InstantiateIn
Dim str As String = ""
str = "<strong>Q.</strong>style='margin-left: 10px;' href='javascript:toggleDiv("
End Sub
End Class
Re: Create Repeater programmatically
Hey,
Short answer is, I don't think you can.
Instead, you should use the ItemDataBound Event:
http://msdn.microsoft.com/en-us/libr...databound.aspx
of the Repeater. This will allow you to set the values within the Repeater ItemTemplate.
Hope that helps!!
Gary
Re: Create Repeater programmatically
Hmm.
Well is there a way to put in a placeholder so I can replace the value? Or would I have to add the template during the databinding and pass the values into class that generates the template?
Re: Create Repeater programmatically
Add the template during databinding, assign an event handler to the repeater's ItemDataBound, and in there, do what you were originally doing. Thing is, the inline code is meant as a quick and dirty way to get data into the template. But you're going down the full-control route of generating repeaters, so there's going to be absolutely no replacement.
You also can't add an eval to the placeholder generated dynamically and hope that it'll get replaced :D Anything that you put in there becomes 'source code' to be generated as HTML to the user's browser, there is no second-pass execution.