031. aps.net中数据绑定控件两种添加自动编号的方法
前端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();
//} } }