0

I'm still trying to understand how I can take advantage from DataBinder ( Is there a way to use a DataBinder.Eval statement as an index of a specific array in an ASPX page? ).

I'm currently building tables with the help of repeater and I would like to use a loop defining the Item label dynamically, to allow more interactions.

Currently, this test code is working:

<asp:Repeater id="Fish" runat="server">
<table>
<ItemTemplate>
  <tr>
    <td><%# Container.DataItem("ITEM")%></td>
    <td><%# Container.DataItem("AGG")%></td>
  </tr>
</ItemTemplate>
</table>
</asp:Repeater>

But as you can imagine, this type of structure doesn't allow to choose dynamically the columns that are displayed from the columns to be ignored.

I was thinking that by using a "for" loop structure, I would be able to choose dynamically which column could be displayed. And I tried this as a test:

Public Test_id() As String
Public Test_idp As String

<% Test_id = New String() {"id", "Agg"} %>
<asp:Repeater id="Fish" runat="server">
<table>
<ItemTemplate>
  <tr>
    <% For Each Test_idp as String In Test_id%>
      <td><%# Container.DataItem(Test_idp)%></td>
    <% Next Test_idp%>
  </tr>
</ItemTemplate>
</table>
</asp:Repeater>

which is not working... and is granted by the following error message:

Overload resolution failed because no Public 'Item' is most specific for these arguments:

'Public Overrides ReadOnly Property Item(name As String) As System.Object': Not most specific.

'Public Overrides ReadOnly Property Item(i As Integer) As System.Object': Not most specific.

Any idea?


Edit:

to answer Mike C's question, I have tried DataBinder.Eval(Container.DataItem, Test_idp) instead of Container.DataItem(Test_idp). It still does not work, but the error is different:

System.ArgumentNullException: value cannot be null

Community
  • 1
  • 1
S3MP
  • 113
  • 7
  • Instead of `Container.DataItem(Test_idp)` have you tried `DataBinder.Eval(Container.DataItem, Test_idp)`? – mclark1129 Aug 20 '12 at 15:45
  • @MikeC I have just tried it - it doesn't work and the error is different. I've updated the description of my example – S3MP Aug 20 '12 at 15:55

2 Answers2

2

Test_Idp is an Object (since it wasn't declared otherwise).

Therefore, the compiler cannot figure out which of those overloads to call.

You need to explicitly declare it As String.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I should have added that I have declared both Test_id and Test_idp as string: Public Test_id() As String and Public Test_idp As String – S3MP Aug 20 '12 at 15:15
  • @S3MP `Test_idp` is NOT declared as a string in your code example. If you have changed it since then, then you need to update your example so that it says `For Each Test_idp As String...`. – mclark1129 Aug 20 '12 at 15:22
  • @MikeC Ok sorry - example updated - tested - still the same error – S3MP Aug 20 '12 at 15:28
0

You can use a nested repeater for the columns.

<asp:Repeater id="Fish" runat="server">
<table>
<ItemTemplate>
  <tr>
    <asp:Repeater id="columns" runat="server">
        <ItemTemplate>
          <td><%# ((RepeaterItem)Container.Parent.Parent).DataItem("ITEM")%></td>
          <td><%# ((RepeaterItem)Container.Parent.Parent).DataItem("AGG")%></td>
        </ItemTemplate>
    </asp:Repeater> 
  </tr>
</ItemTemplate>
</table>
</asp:Repeater>
Magnus
  • 45,362
  • 8
  • 80
  • 118