转自:http://my.oschina.net/dldlhrmc/blog/93458
前台代码
01 |
<%@ Page Language= "C#" AutoEventWireup= "true" CodeFile= "Default5.aspx.cs" Inherits = "Default5" %> |
03 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > |
05 |
<html xmlns= "http://www.w3.org/1999/xhtml" > |
10 |
<form id= "form1" runat= "server" > |
13 |
<asp:GridView ID= "GridView1" runat= "server" AutoGenerateDeleteButton= "True" |
14 |
AutoGenerateEditButton= "True" DataKeyNames= "id" |
15 |
onrowdeleting= "GridView1_RowDeleting" onrowediting= "GridView1_RowEditing" |
16 |
onrowupdating= "GridView1_RowUpdating" |
17 |
onrowcancelingedit= "GridView1_RowCancelingEdit" > |
后台代码
02 |
using System.Collections.Generic; |
06 |
using System.Web.UI.WebControls; |
07 |
using System.Data.SqlClient; |
10 |
public partial class Default5 : System.Web.UI.Page |
12 |
private void BindData() |
14 |
String Strcon = "Data Source=.;Initial Catalog=testDB;Integrated Security=True" ; |
15 |
SqlConnection con = new SqlConnection(Strcon); |
16 |
String sql = "select userinfo.id,username,password,birthday from userinfo" ; |
17 |
SqlDataAdapter ad = new SqlDataAdapter(sql, con); |
18 |
DataSet ds = new DataSet(); |
20 |
GridView1.DataSource = ds; |
24 |
protected void Page_Load(object sender, EventArgs e) |
26 |
if (!IsPostBack) /*如果省略这句,下面的更新操作将无法完成,因为获得的值是不变的*/ |
31 |
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) |
33 |
String Strcon = "Data Source=.;Initial Catalog=testDB;Integrated Security=True" ; |
34 |
SqlConnection con = new SqlConnection(Strcon); |
35 |
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);/*获取主键,需要设置 DataKeyNames,这里设为 id */ |
36 |
String sql = "delete from userinfo where id='" + id+ "'" ; |
38 |
SqlCommand com = new SqlCommand(sql, con); |
40 |
com.ExecuteNonQuery(); |
46 |
/*编辑操作,利用e.NewEditIndex获取当前编辑行索引*/ |
47 |
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) |
49 |
GridView1.EditIndex = e.NewEditIndex; |
50 |
BindData(); /*再次绑定显示编辑行的原数据,不进行绑定要点2次编辑才能跳到编辑状态*/ |
52 |
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) |
54 |
String Strcon = "Data Source=.;Initial Catalog=testDB;Integrated Security=True" ; |
55 |
SqlConnection con = new SqlConnection(Strcon); |
56 |
String username = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text.ToString(); /*获取要更新的数据*/ |
57 |
String password = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text.ToString(); |
58 |
String birthday = (GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text.ToString(); |
60 |
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);/*获取主键,需要设置 DataKeyNames,这里设为 id */ |
62 |
String sql = "update userinfo set username='" + username + "',password='" +password+ "',birthday='" +birthday+ "' where id='" +id+ "'" ; |
64 |
SqlCommand com = new SqlCommand(sql, con); |
66 |
com.ExecuteNonQuery(); |
68 |
GridView1.EditIndex = -1; |
71 |
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) |
73 |
GridView1.EditIndex = -1; /*编辑索引赋值为-1,变回正常显示状态*/ |
SQL 脚本(SQL Server)
04 |
/****** Object: Table [dbo].[userinfo] Script Date : 12/02/2012 22:20:46 ******/ |
08 |
SET QUOTED_IDENTIFIER ON |
14 |
CREATE TABLE [dbo].[userinfo]( |
15 |
[id] [ int ] IDENTITY(1,1) NOT NULL , |
16 |
[username] [ varchar ](50) NULL , |
17 |
[ password ] [ varchar ](50) NULL , |
18 |
[birthday] [ varchar ](50) NULL , |
19 |
CONSTRAINT [PK_userinfo] PRIMARY KEY CLUSTERED |
22 |
) WITH (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [ PRIMARY ] |
本人菜鸟,勿喷!仅供参考。
- GridView总结二:GridView自带编辑删除更新
GridView自带编辑删除更新逻辑很简单:操作完,重新绑定.总结总结,防止忘记... 效果图: 前台代码: <%@ Page Language="C#" AutoEvent ...
- GridView编辑删除操作
第一种:使用DataSource数据源中自带的编辑删除方法,这样的不经常使用,在这里就不加说明了. 另外一种:使用GridView的三种事件:GridView1_RowEditing(编辑).Grid ...
- GridView编辑删除
A前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.asp ...
- GridView 编辑,更新,删除 等操作~~
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1. ...
- Asp.Net:GridView 编辑、删除、自定义分页以后备用
页面 GridView 绑定:在中,有 <asp:BoundField/>和 <asp:TemplateField><ItemTemplate>嵌套服务器控件 &l ...
- ASP.NET编辑与更新数据(非GridView控件实现)
Insus.NET在实现<ASP.NET开发,从二层至三层,至面向对象 (5)>http://www.cnblogs.com/insus/p/3880606.html 中,没有把数据编辑与 ...
- gridview的编辑,更新,取消,自动分页等
gridview编辑列,把左下角的"自动生成字段"的复选框的勾去掉 添加boundfield(绑定列)将其datafield设置为productname,headertext设置为 ...
- GridView 编辑、删除 、分页
类似代码都差不多,记录一下,便于以后查看使用. 前台页面: <asp:GridView ID="gdvList" runat="server" AutoG ...
- asp gridview批量删除和全选
本人新手刚学asp.net 全选和删除也是引用了他人的代码试了一试可以实现,感觉很好,就发了上来. 前台代码 <asp:GridView ID="GridView1" r ...
- GridView编辑、取消按钮自定义控件
这个需求来自于论坛一位坛友提出的问题,他希望能够自定义编辑.取消按钮,而不是用GridView自带的编辑和取消.这里只当抛砖引玉,提出一些解决方案. 首先在页面前台设置一个GridView. < ...
随机推荐
- LM2596扩流
- java web-----DAO设计模式(数据库访问)
一,DAO设计模式用于 j2ee 的数据层访问,包括五部分, 数据库连接类(包含数据库的连接与关闭操作的一个类), VO类(私有变量与数据库表格对应,接收数据库中表格各字段内容), DAO接口类(包含 ...
- 【BZOJ3884】【降幂大法】上帝与集合的正确用法
Description 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“元” ...
- Object-API-NSLog
NSLog中的基础数据类型 输出格式: NSLog("") char: %c short int: %hi %hx %ho unsigned short int: %hu %hx ...
- nginx 502
查过网上的资源,基本都是认为是php线程打开文件句柄受限导致的错误.具体的解决的办法如下: 1.提升服务器的文件句柄打开打开 /etc/security/limits.conf : (增加) * ...
- Java中静态代码块,代码块,构造方法的理解
直接贴代码 class A { static { System.out.println("父类静态代码区"); } { System.out.println("父类代码区 ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 初识pngdrive
初识是第一次认识的意思,类似的词还有初见.初遇.初心.初愿.初恋.初吻……梦里相见如初识,很美好的感觉.同样,今天我们要认识的也是一个比较神奇美妙的东西,至少对于程序员来说. 我曾经尝试过很多文件加密 ...
- php中引用符号(&)的使用详解
php的引用就是在变量或者函数.对象等前面加上&符号,在PHP 中引用的意思是:不同的名字访问同一个变量内容,下面介绍如何使用PHP的引用 与C语言中的指针是有差别的.C语言中的指针里面存储的 ...
- Uniqueidentifier数据类型
一.Uniqueidentifier数据类型 可存储16字节的二进制值 Uniqueidentifier用来存储一个全局唯一标识符,即GUID.GUID是唯一的二进制数:世界上的任何两台计算机都不会生 ...