转自: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. < ...
随机推荐
- java新手笔记11 类的静态属性、方法(单例)
1.Person类 package com.yfs.javase; public class Person { String name;//每个对象上分配 与对象绑定 int age; char se ...
- java PropertyChangeSupport委托帧听类的使用
要委托的Bean类写法. import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import ...
- Java RandomAccessFile的使用(转载的文章,出处http://www.2cto.com/kf/201208/149816.html)
Java的RandomAccessFile提供对文件的读写功能,与普通的输入输出流不一样的是RamdomAccessFile可以任意的访问文件的任何地方.这就是“Random”的意义所在. Rando ...
- 递归查找某个目录下是否存在NOTICE文件
从Catalogs.txt文件中,读取待检查的目录列表.检查这些目录中,是否存在NOTICE文件,如果没有则检查它的父目录,直到cd ..到Repository目录. 如果cd ..到Reposito ...
- 认识linux权限
首先,我们来了解下linux系统的用户和用户组 场景:公司里有两个项目组:小组A和小组B:A.B.C是小组A的成员,甲.乙是小组B的成员.为了保密起见,小组内的进度.文档.程序都有小组内公开.比如小组 ...
- jquery 之效果
// jquery 之效果 .css()既可以获取值,如 .css('fontSize'), 又可以设置内置属性,既可用驼峰式,也可以用连字符版,如 .css('background-color', ...
- 解决DataGridView.DataSource重复赋值而不显示问题
List<Person> list=new List<Person>(); ;i<;i++) { list.Add(new Person(){........}) } d ...
- PHP异常处理
一.异常处理——可以有效地控制多条出现错误或异常的代码 基本语法如下: try{ //可能出现异常的代码 } catch(Exception $e){ //对异常处理 //1.自己处理 //2.不作处 ...
- POJ 3349 Snowflake Snow Snowflakes Hash
题目链接: http://poj.org/problem?id=3349 #include <stdio.h> #include <string.h> #include < ...
- win7系统cocos2dx 3.4 绑定自定义类到Lua
Cocos2d-x 3.0开始使用bindings-generator来生成c++类的lua绑定.bindings-generator基于tolua++,通过配置tools/tolua中的ini文件以 ...