前端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();
        //}
}
}

031. aps.net中数据绑定控件两种添加自动编号的方法的更多相关文章

  1. 027. asp.net中数据绑定控件之 GridView控件

    GridView控件支持下面的功能: 绑定至数据源控件, 如SqlDataSource 内置排序功能 内置更新和删除功能 内置分页功能 内置行选择功能 可以编程方式访问GridView对象模型以动态设 ...

  2. 【MFC】picture控件 两种有细微差别的动态加载图片方法

    摘自:http://www.jizhuomi.com/software/193.html VS2010/MFC编程入门之二十七(常用控件:图片控件Picture Control) 分类标签: 编程入门 ...

  3. VFP中OCX控件注册检测及自动注册

    这是原来从网上搜集.整理后编制用于自己的小程序使用的OCX是否注册及未注册控件的自动注册函数. CheckCtrlFileRegist("ctToolBar.ctToolBarCtrl.4& ...

  4. ASP.NET数据绑定控件

    数据绑定控件简介 数据绑定分为:数据源 和 数据绑定控件 两部分,数据绑定控件通过数据源来获得数据,通过数据源来隔离数据提供者和数据使用者,数据源有:SqlDataSource,AccessDataS ...

  5. ASP.NET数据绑定控件简介

    •数据绑定分为数据源和数据绑定控件两部分(①数据绑定控件通过数据源获取和修改数据②数据绑定控件通过数据源隔离数据提供者和数据使用者)数据绑定控件→数据源→数据库•数据源:SqlDataSource(连 ...

  6. WPF中TreeView控件数据绑定和后台动态添加数据(二)

    写在前面:在(一)中,介绍了TreeView控件MVVM模式下数据绑定的方法.在这篇文章中,将总结给节点添加事件的方法,这样说有些不对,总之实现的效果就是点击某个节点,将出现对应于该节点的页面或者数据 ...

  7. 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误

    原文:如何解决FormView中实现DropDownList连动选择时出现 "Eval().XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的 ...

  8. [C#]WinForm 中 comboBox控件之数据绑定

    [C#]WinForm 中 comboBox控件之数据绑定 一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<s ...

  9. [转]MFC子线程中更新控件内容的两种办法

    一.概述 每个系统中都有线程(至少都有一个主线程),而线程最重要的作用就是并行处理,提高软件的并发率.针对界面来说,还能提高界面的响应能力.一般的,为了应用的稳定性,在数据处理等耗时操作会单独在一个线 ...

随机推荐

  1. C:上台阶

    总时间限制: 1000ms 内存限制: 65536kB描述楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可以一步上2阶,也可以一步上3阶,编程计算共有多少种不同的走法. ...

  2. 基于天天动听API开发在线音乐查询网站

    预览图 源码下载 地址:https://github.com/bajdcc/dev/tree/master/ttpod 文件夹说明: App_Code,WCF服务 Script,离线下载的celery ...

  3. OpenCV源码分析:RGB到其他色彩空间的转换

    1.流程调用图 2.部分代码分析 //模板函数进行颜色空间的转换 template <typename Cvt> void CvtColorLoop(const Mat& src, ...

  4. 利用JAVA想数据库中提交数据

    1.用户信息提交界面 <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...

  5. 配置Entity Framework连接Sql Server出现的一个异常

    异常:The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFrame ...

  6. echart------属性详细介绍

    theme = { // 全图默认背景 // backgroundColor: 'rgba(0,0,0,0)', // 默认色板 color: ['#ff7f50','#87cefa','#da70d ...

  7. [vijos P1391] 想越狱的小杉

    考前最后一题,竟然是第一次码SPFA,虽然这个算法早有耳闻,甚至在闻所未闻之前自己有过一个类似的想法,说白了就是广搜啊,但是敲起来还是第一次啊,而且这还不是真正意义上的SPFA. 完全按照自己想法来码 ...

  8. python操作excel表格详解(xlrd/xlwt)

    http://www.2cto.com/kf/201501/373655.html http://blog.csdn.net/b_h_l/article/details/17001395 利用pyth ...

  9. Android Studio项目目录结构

    在Android Studio中,提供了以下几种项目结构类型 我们一般常用的有以下两种结构: Project 结构类型 app/build/ app模块build编译输出的目录 app/build.g ...

  10. 《Java中的单例模式--两种》

    //单例模式:饿汉式和懒汉式 //单例模式 /* 设计模式:解决某一类问题行之有效的解决办法(思想). 单例(Singleton):设计模式: 学习设计模式必须先弄清楚它是解决什么问题的. 单例模式是 ...