GridView,Repeater增加自动序号列
有三种实现的方式,
第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了.
<Columns>
<asp:TemplateField HeaderText="序号" InsertVisible="False">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="5%" />
<ItemTemplate>
<%#Container.DataItemIndex+1%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
第二种方式分页时进行了计算,这样会累计向下加.
<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();
}
}
第四种方法
双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果是绑定数据行 //清清月儿http://blog.csdn.net/21aspnet
if (e.Row.RowType == DataControlRowType.DataRow)
{
////鼠标经过时,行背景色变
//e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
////鼠标移出时,行背景色变
//e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
////当有编辑列时,避免出错,要加的RowState判断
//if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
//{
// ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
//}
}
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
}
注意这时最好把前台的第一列的表头该为“编号”,因为以前的第一列被“吃掉”了。
<asp:GridView ID="GridView1" runat="server" CellPadding="3" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound">
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
<asp:BoundField DataField="姓名" HeaderText="用户姓名" />
<asp:BoundField DataField="员工性别" HeaderText="性别" />
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
这里基本上介绍的已经够用了,详细试试;
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>
就可以了。
%>%>
</ItemTemplate>
</asp:Repeater>
赠送一个小技巧:
Repeater数据为空时提示暂无数据
<FooterTemplate>
<asp:Label ID="lblEmpty" Text="暂无数据" runat="server" Visible='<%#bool.Parse((Repeater1.Items.Count==0).ToString())%>'></asp:Label>
</FooterTemplate>
GridView,Repeater增加自动序号列的更多相关文章
- aspxgridView,Repeater增加自动序号列
第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了. <asp:TemplateField HeaderText="序号" Ins ...
- ASP.NET(C#)--Repeater中生成“序号”列
需求介绍:在Repeater(Table)中加入“序号”列,从1开始自增,步长为1. 思路:因为“序号”跟Repeater的行号有关,所以要在Repeater的ItemDataBound事件中输出“序 ...
- GridView,datalist添加序号列
GridView添加序号列:这个是经常需要的一个功能 <asp:TemplateField HeaderText="序号"> <ItemTemplate> ...
- DataList分页-增加自动编号列
<asp:DataList ID="dl_XUDAXIA" runat="server"> <HeaderTemplate> <t ...
- Oracle使用row_number()函数查询时增加序号列
使用Oracle自带的row_number()函数能够实现自动增加序号列的要求,但是同时引发一个问题,如果我们查询出来的数据需要使用Order By排序的话,那么我们会发现新增加的序号列是乱序的,它会 ...
- .Net GridView 序号列
给GridView增加一列:序号列 <asp:TemplateField HeaderText="序号"> <ItemTemplate> <%# (( ...
- ASP.NET 为GridView添加序号列,且支持分页连续累计显示
为GridView添加序号列,且支持分页连续累计显示,废话不多说,直接上代码: <%@ Page Language="C#" AutoEventWireup="tr ...
- 使用模板技术处理ASP.NET中GridView额外序号列的问题
问题描述: 现在要在一张GridView表中添加一列来显示行号,要求是显示下一页的时候能够递增而不是从新编号.数据库中的没有相关序号列 如何在软件端实现呢? 通过测试,添加以下代码即可解决需求. &l ...
- ## GridView 布局:item设置的高度和宽度不起作用、自动适配列数、添加Header和Footer ##
一.item设置的高度和宽度不起作用 转自:http://www.cnblogs.com/0616--ataozhijia/p/6031875.html [Android Pro] listView和 ...
随机推荐
- thinkphp3.2与phpexcel基础生成
public function ff(){ import("Org.Util.PHPExcel"); import("Org.Util.PHPExcel.Workshee ...
- marquee标签,好神奇啊...
<html><body><div style="height:190; margin-top:10; margin-bottom:10; width:96%; ...
- eclipse中 linked resource的使用
一.关于linked resource eclipse 中的linkded resources 是指存放在项目所在位置以外某个地方的文件或者文件夹:这些特定的资源必须有一个项目作为他们的父资源.l ...
- 问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
错误提示: psql: could not connect to server: No such file or directory Is the server running locally and ...
- Express知识整理
开发实例 Express开发实例(1) —— Hello,world! Express开发实例(2) —— Jade模板引擎
- hzwer模拟赛 虫洞
[题目描述] N个虫洞,M条单向跃迁路径.从一个虫洞沿跃迁路径到另一个虫洞需要消耗一定量的燃料和1单位时间.虫洞有白洞和黑洞之分.设一条跃迁路径两端的虫洞质量差为delta. 1.从白洞跃迁到黑洞,消 ...
- PAT mooc DataStructure 4-2 SetCollection
数据结构习题集-4-2 集合的运用 1.题目: We have a network of computers and a list of bi-directional connections. Eac ...
- Wireshark抓包工具
首先下载并安装Wireshark软件,最好选择中文版,因为会使你用的更顺手. 安装完毕之后,双击打开Wireshark软件,主界面还是比较清晰明了的,可是怎么用还是稀里糊涂的吧. 点击菜单栏红圈中的选 ...
- JQ图片轮播
<script src="{staticurl action="jquery.js" type="js"}"></scri ...
- 转一篇介绍Web session概念的文章
说的非常好,如果你对web中的session概念不太清楚的话,一定要去看看,可能需要FQ才能阅读:http://fred-zone.blogspot.com/2014/01/web-session.h ...