转自: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. < ...
随机推荐
- IOS之沙盒(Sandbox)机制
IOS中每个App应用程序都有一个单独封闭的文件夹,这个文件夹称为沙盒,并且苹果规定,任何App都无权访问其他App的沙盒 沙盒目录通过 FOUNDATION_EXPORT NSString *NSH ...
- 在js中window.open通过“post”传递参数
在js中window.open通过“post”传递参数的步骤如下: 如:在A.jsp中 有一个js方法 winow.open,目标地址是 xx.do 1.在A.jsp建一个form,把要设置的值通过j ...
- eclipse+PyDev 中报错"scrapy.spiders.Spider" ,可用"# @UndefinedVariable"压制.
# -*- coding:utf-8 -*- ''' Created on 2015年10月22日 (1.1) 例子来源: http://scrapy-chs.readthedocs.org/zh_C ...
- Java学习笔记(基本数据类型和变量命名规则)
java基本数据类型 变量 1.变量就是可变的量. 2.常量就是不可变的量. 3.字面量:Java的变量和常量中存放的具体的数据成为字面量. 变量 命名规则: (1)首字母是英文字母.$或下划线,由字 ...
- Core模块其他常用知识点[OpenCV 笔记14]
Matx 轻量级的Mat,必须在使用前规定好大小,比如一个2x3的float型的Matx,可以声明为Matx23f Vec Vec是Matx的一个派生类,是一个一维的Matx,跟vector很相似.在 ...
- 【实习记】2014-08-27堆排序理解总结+使用typedef指代函数指针
过程记录 4个月前C语言版的七大排序算法实践让我在写C++版时轻车熟路.特别是冒泡,插入,希尔,选择这四种排序不用调试即运行成功.输出的效果与C语言做的版本完全一样,其中令我印象深刻的是,co ...
- 忘记mysql的root密码
如果忘记root密码或其他用户密码,不要急,按下面操作即可.1. 编辑mysql主配置文件 my.cnfvim /etc/my.cnf 在[mysqld]字段下添加参数 skip-grant ...
- u-boot和linux的机器码
先看u-boot的机器码和linux的机器码是在什么地方决定的. 1. u-boot的机器码是在u-boot的board/fs2410/fs2410.c文件里决定的: /* arch numb ...
- MySQL数据库的热备份和冷备份
冷备份(off, 慢, 时间点上恢复)冷备份发生在数据库已经正常关闭的情况下,当正常关闭时会提供给我们一个完整的数据库.冷备份是将关键性文件拷贝到另外位置的一种说法.对于备份数据库信息而言,冷备份是最 ...
- DelphiXE4- System.IOUtils.TDirectory笔记查询后缀名为dll的文件
TStringDynArray 在System.Types中定义