I have been trying to use declarative data bound ASP.NET, and struggling with the "magic" that goes on behind the scenes.
How do I do get this to work in ASP.Net 2.0. This is inside a data bound FormView
control.
<asp:Label ID="RecipientsLabel" runat="server"
Text='<%# String.Join("; ", Eval("HtmlEncodedRecipients")) %>'>
</asp:Label>
Note: HtmlEncodedRecipients
is a List<string>
property.
I get this error:
The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments
I've found life is a lot easier if you just cast the DataItem to the type you're expecting. This makes intellisense kick in so you can see what's going on.
((MyObject)Container.DataItem).HtmlEncodedRecipients
If it's a List then this is what you want:
String.Join("; ", ((List<string>)Eval("HtmlEncodedRecipients")).ToArray())
(The thing to remember is that if you put an Eval in a databinding string by itself, it will come up with a "nice" value to show. But if you use it in an expression, it returns type object, so you have to cast it.
That's getting a bit too gnarly to put inline, though, so do this:
Text='<%# ConvertRecipients(Eval("HtmlEncodedRecipients")) %>'>
and in your codebehind
string[] ConvertRecipients(object recipientObject)
{
if(recipientObject is List<string>)
{
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With