Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use Repeater Item index in Hidden Field

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>
like image 664
Dinesh Sharma Avatar asked Jan 29 '26 23:01

Dinesh Sharma


2 Answers

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();
        }
    }
like image 62
Brian Scott Avatar answered Feb 01 '26 13:02

Brian Scott


it has to be like this:

Value="<%# DataBinder.Eval(Container, "ItemIndex") %>"
like image 44
Grigor Avatar answered Feb 01 '26 13:02

Grigor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!