简体   繁体   中英

how to manipulate comma separated string from list<string[]> binding in aspx page?

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?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM