• aspxgridView,Repeater增加自动序号列


    第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了.

    <asp:TemplateField HeaderText="序号" InsertVisible="False">
    <ItemStyle HorizontalAlign="Center" />
    <HeaderStyle HorizontalAlign="Center" Width="5%" />
    <ItemTemplate>
    <%#Container.DataItemIndex+1%>
    </ItemTemplate>
    </asp:TemplateField>
    第二种方式分页时进行了计算,这样会累计向下加.

    <asp:TemplateField HeaderText="序号" InsertVisible="False">
    <ItemStyle HorizontalAlign="Center" />
    <HeaderStyle HorizontalAlign="Center" Width="5%" />
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text="<%# this.MyListGridView.PageIndex * this.MyListGridView.PageSize + this.MyListGridView.Rows.Count + 1%>"/>
    </ItemTemplate>
    </asp:TemplateField>

    还有一种方式放在cs代码中,和第二种相似.

    <asp:BoundField HeaderText="序号" >
    <ItemStyle HorizontalAlign="Center" />
    <HeaderStyle HorizontalAlign="Center" Width="5%" />
    </asp:BoundField>
    protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowIndex != -1)
    {
    int indexID = this.myGridView.PageIndex * this.myGridView.PageSize + e.Row.RowIndex + 1;
    e.Row.Cells[0].Text = indexID.ToString();
    }
    }

    Repeater自身就带有这个获取当前行号的属性,而无需程序员绑定这个行号,Container.ItemIndex 就可以获取了,见下示例:

    <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
    行号:<%#Container.ItemIndex %>
    </ItemTemplate>
    </asp:Repeater>

    如果上面的示例中,Repeater已经绑定了数据,并且数据的至少为一笔记录,那么行号就会显示出来,行号从零开始,如果想改为从1开始,那么可以将以上的代码改为Container.ItemIndex + 1,见如下示例:

    <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
    行号:<%#Container.ItemIndex + 1 %>
    </ItemTemplate>
    </asp:Repeater>

    就可以了。

    <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
    <%# Container.ItemIndex + 1%>
    <%# (Container as RepeaterItem).ItemIndex + 1%>
    </ItemTemplate>
    </asp:Repeater>

    我还是会相信,星星会说话,石头会开花,穿过夏天的栅栏和冬天的风雪过后,你终会抵达。
  • 相关阅读:
    最长公共子序列
    最长重复子串—后缀数组
    最长递增子序列
    最长重复子串(转)
    最长递增子序列(转)
    最长不重复子串(转)
    连续子数组最大和(转)
    alert 与 console.log
    一个null,让浏览器SB
    javascript实现简单的动画功能
  • 原文地址:https://www.cnblogs.com/dfxyw/p/5080089.html
Copyright © 2020-2023  润新知