repeater binding to method?

G

Guest

I have an array of objects I'm binding to a Repeater. Unfortunately, I
need to bind to the value returned from a method, while Eval() only seems to
work on Properties.
Is there a way to do this?
Can I intercept an Event to provide a value?
 
G

Grant Merwitz

You can use the ItemDataBound Event, and cast your Item as its Object.
Then set a Label in your repeater to that string returned by the method

So Repeater:

<asp:Repeater id="rpt">
<ItemTemplate>
<asp:Label id=lbl runat="server"></asp:Label>
<ItemTemplate>
</asp:Repeater>

In your code behind

private void rpt_OnItemDataBound(object Sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemTemplate != ListItemType.Header && e.Item.ItemTemplate
!= ListItemType.Footer)
{
//Lets say your Object is name MyObject
MyObject thisObject = (MyObject)e.Item.DataItem; //This is
sometimes a bit tricky, hope its right
string StrFromMethod = thisObject.GetStringMethod();
((Label)e.Item.FindControl("lbl")).Text = StrFromMethod;
}
}

HTH
 
G

Guest

....

I was able to add an indexer that took the one param I had and have it call
the method. This is not a general solution, but fits in this case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top