Reputation: 14269
I have a repeater control in aspx page and I am binding it to a list<string[]>
If i wanted to display repeater item at index 10 of the string array i would do the following;
<%# ((string[])(Container.DataItem))[10] %>
Which works correctly and as expected.
Now in <%# ((string[])(Container.DataItem))[18] %>
it returns a comma separated string “item1, 1, item2, 2, item3, 3”
and I want to be able to display and control each item in the string.
I tried using
<% foreach (var v in (((string[])(Container.DataItem))[18]).Split(','))
{
//do something
}
%>
But I will get this error: The name 'Container' does not exist in the current context
And I tried using
<%# foreach (var v in (((string[])(Container.DataItem))[18]).Split(','))
{
//do something
}
%>
But I will get the following error: Invalid expression term 'foreach'
So, how am I supposed to be able to bind the comma separated sting while being able to manipulate the data?
Upvotes: 0
Views: 921
Reputation: 62301
If you want to do something with data, you want to let a method to handle it.
Here is an example -
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%# DoSomething(((string[])(Container.DataItem))[18]) %>
</ItemTemplate>
</asp:Repeater>
protected string DoSomething(object value)
{
var sb = new StringBuilder();
foreach (var item in value.ToString().Split(','))
{
sb.Append(item + " - ");
}
return sb.ToString();
}
Upvotes: 1