zoukankan      html  css  js  c++  java
  • 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>

    我还是会相信,星星会说话,石头会开花,穿过夏天的栅栏和冬天的风雪过后,你终会抵达。
  • 相关阅读:
    AD域服务器的部署 【1】— AD域介绍
    Docker 设置http代理
    在Django中将SQLite3数据库迁移到MySQL
    pycharm远程更新代码到远端服务器
    这个看着更好。Docker中使用MySQL
    docker换成最好用的源
    docker基础命令
    在docker中运行mysql实例
    centos7安装mysql
    centos 7 修改ip
  • 原文地址:https://www.cnblogs.com/dfxyw/p/5080089.html
Copyright © 2011-2022 走看看