Can I Number a GroupTemplate or ItemTemplate?

喜夏-厌秋 提交于 2019-12-04 16:58:37

I don't think you can DataBind the id, so I'd probably either use a hidden field, have JQuery count them up, or use the CssClass. You can use Container.DataItemIndex to get your number.

Edit: By just changing the ul to be runat="server", ASP.NET will generate a unique id for you in it's infamous INamingContainer format. That will include an index as well, though it'll be something like lv_ctrl0_group, and is an implementation detail.

You could hook a handler to the ul's Init event and append a number to it, making it something like lv_ctrl0_group1. I don't think you can get rid of the prepended INamingContainer stuff very easily, and this would probably break any IPostDataHandler controls.

<script runat="server">
    void Group_Init(object sender, EventArgs e) {
       ((Control)sender).ID += groupId++.ToString();
    }
    int groupId = 0;
</script>

<asp:ListView id="lv" runat="server" GroupItemCount="3">
    <LayoutTemplate>
        <asp:PlaceHolder ID="groupPlaceHolder" runat="server" />
    </LayoutTemplate>
    <GroupTemplate>
        <ul id="group" runat="server" oninit="Group_Init">
            <asp:PlaceHolder ID="itemPlaceHolder" runat="server"/>
        </ul>
    </GroupTemplate>
    <ItemTemplate>
        <li>Item</li>
    </ItemTemplate>
</asp:ListView>

In your aspx file:

<GroupTemplate>
  <ul id='<%# "group"+GroupNumber %>'>
    <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
  </ul>
</GroupTemplate>

In your code behind (assuming C#):

int _GroupNumber=0;

protected string GroupNumber
{
   get { return (++_GroupNumber).ToString(); }
}

The solution from Keltex above would work with a small change; use <%= instead <%#,

<%# wouldnt work because GroupTemplate doesnt support databinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!