开发者

use Repeater Item index in Hidden Field

开发者 https://www.devze.com 2023-03-04 21:51 出处:网络
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.

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" Tex开发者_C百科t='<%# DataBinder.Eval(Container.DataItem, "ACM_ACCOUNT_DESC")%>'></asp:Label>
        </td>
    </tr>
</ItemTemplate>


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


it has to be like this:

Value="<%# DataBinder.Eval(Container, "ItemIndex") %>"


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消