前端HTML代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" MaintainScrollPositionOnPostback="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>在GridView控件中实现自动编号</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" border="1" style="font-size: 9pt">
<tr>
<td style="font-size: 9pt; color: #ff0000; text-align: center">
商品信息</td>
</tr>
<tr>
<td style="text-align: center">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDataBound="GridView1_RowDataBound" PageSize="4">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="AutoNum" HeaderText="自动编号" />
<asp:BoundField DataField="cardNo" HeaderText="会员卡号" />
<asp:BoundField DataField="name" HeaderText="姓名" />
<asp:BoundField DataField="cardBound" HeaderText="内卡号" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="addr" HeaderText="地址" />
<asp:BoundField DataField="email" HeaderText="邮箱" />
</Columns>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Right" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<i>您当前正在查看的页码:<b><font color="#ff0000"><%=GridView1.PageIndex + 1%>/<%=GridView1.PageCount%>
</font></b></i>
</td>
</tr>
</table> </div>
</form>
</body>
</html>

后台对应的CS文件代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon;
string strCon = "server=.;uid=sa;pwd=123.456;database=TYW";
protected void Page_Load(object sender, EventArgs e)
{
//这是一种通过数据库级别上的编号自动生成原则实现, 和前端的控件基本上没有关系
string sqlstr = "select ROW_NUMBER() OVER(ORDER BY cardno) AS AutoNum,* from card";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr,sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds);
sqlcon.Close();
GridView1.DataSource = myds;
GridView1.DataBind();
sqlcon.Close();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        //实现在数据绑定层面的自动编号
        //if (e.Row.RowIndex != -1)
        //{
        //    int id = e.Row.RowIndex + 1;
        //    e.Row.Cells[0].Text = id.ToString();
        //}
}
}

最新文章

  1. python 聊天室
  2. asp.net identity 3.0.0 在MVC下的基本使用 序言
  3. C# 小例子
  4. String类字符串截取示范
  5. Oracle知识整理
  6. 关于arguments对象以及函数的柯里化;
  7. React Native之ViewPagerAndroid跳转页面问题
  8. 关于android布局的两个属性dither和tileMode
  9. BZOJ 3211: 花神游历各国( 线段树 )
  10. CTE递归 MAXRECURSION 遇到的问题
  11. jenkins 从git拉取代码
  12. FJUT16级第一周寒假作业题解D题
  13. SoC嵌入式软件架构设计
  14. List 去重
  15. Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager 转发非原创
  16. CSS外观属性
  17. Node buffer模块缓冲区
  18. Casper带有4个内建记录级别
  19. OpenID Connect Core 1.0(一)介绍
  20. ios 显示代码块(show the code snippet library)

热门文章

  1. nginx rewrite 指令last break区别最详细的解释
  2. PDF 补丁丁 0.4.2.1063 测试版发布:新增检查新版本功能
  3. c#网络通信框架networkcomms内核解析 序言
  4. C#/ASP.NET MVC微信公众号接口开发之从零开发(三)回复消息 (附源码)
  5. 1095: [ZJOI2007]Hide 捉迷藏
  6. &lt;1 小玩意(覆盖效果)
  7. AngularJS学习笔记(1)
  8. kernel source reading notepad
  9. Get IP Address in Android 4.0+
  10. SharePoint\O365 CSOM操作&quot;请求访问设置&quot;功能