简体   繁体   English

如何操作列表中逗号分隔的字符串 <string[]> 绑定在aspx页面?

[英]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; 我在aspx页面中有一个转发器控件,并将其绑定到list<string[]>如果我想在字符串数组的索引10处显示转发器项,我将执行以下操作:

<%# ((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. 现在在<%# ((string[])(Container.DataItem))[18] %>它返回一个逗号分隔的字符串“item1, 1, item2, 2, item3, 3” ,我希望能够显示和控制字符串中的每个项目。 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' 但是我会收到以下错误: 无效的表达式术语'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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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