2

How to set Item index in a Repeater Column with Hidden Fields in asp.net.
I wrote the following code but it does not show the index value in hidden field.

<ItemTemplate>
    <tr>
        <td>
            <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Container.ItemIndex+1 %>' />
            <%--<asp:HiddenField ID="hfIndex" runat="server" Value='<%#Container.ItemIndex+1 %>' />--%>
        </td>
        <td>
            <asp:Label ID="lblExpenseGLCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ACM_ACCOUNT_CODE")%>'></asp:Label>
        </td>
        <td>
            <asp:Label ID="lblAccountGLDescription" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ACM_ACCOUNT_DESC")%>'></asp:Label>
        </td>
    </tr>
</ItemTemplate>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Dinesh Sharma
  • 117
  • 4
  • 5
  • 16

3 Answers3

5

I can't see anything necessarily wrong with the itemtemplate, it would probably help if you provide the entire repeater code as a direct copy / paste in case there area any other errors.

Another approach you can take is to add a repeater data item bound event and then capture the hiddenfield in the event. Once you have the hidden input you can simply set it's value.

Repeater code:

<asp:Repeater ID='myRepeater' runat="server" OnItemDataBound='myRepeater_OnItemDataBound'>
      <ItemTemplate>
        <asp:HiddenField ID='myHidden' runat="server" />
      </ItemTemplate>
</asp:Repeater>

And the code behind event:

protected void myRepeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var myHidden = (HiddenField)e.Item.FindControl("myHidden");
            myHidden.Value = e.Item.ItemIndex.ToString();
        }
    }
Brian Scott
  • 9,221
  • 6
  • 47
  • 68
3

it has to be like this:

Value="<%# DataBinder.Eval(Container, "ItemIndex") %>"
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Grigor
  • 31
  • 1
1

try following:

<asp:Repeater ID="myRepeater" runat="server">
<HeaderTemplate>
    <table>
</HeaderTemplate>
<ItemTemplate>
    <tr>
        <td><%# Container.ItemIndex %></td>
        <!-- or maybe -->
        <td><%# Container.ItemIndex + 1 %></td>
        <td><%# DataBinder.Eval(Container, "DataItem.Name") %></td>
    </tr>
</ItemTemplate>
<FooterTemplate>
    </table>
</FooterTemplate>
</asp:Repeater>
Mohamed Badawey
  • 101
  • 1
  • 1
  • 8