转自: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. < ...
随机推荐
- IBM WebSphere MQ 通道类型配置
IBM WebSphere MQ 通道类型配置 初学MQ,四种常见通道,windows下操作 目录 Sender--Receiver Server-Receiver Server-Requester ...
- 时空分割的画面--用xcode命令行回忆turbo c
大学时期曾经玩过turbo c的同学,可以用xcode命令行写写c程序,回味一下吧:) 1. 首先在终端输入,touch main.c 新建文件 2. 编辑main.c内容,写一段简单代码 #incl ...
- DbUtil组件及C3P0数据库连接池组件的使用
DbUtils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能. 使用c ...
- php快递单号查询源码
贴下记录下php查询快递单号的源码,能查询各种快递的快递单号记录,中通.圆通快递.顺丰快递等都不是问题.只需要在 爱快递(www.aikuaidi.cn)上面申请一个快递key即可,下面把源码分享下, ...
- Vim 中文件目录浏览插件——NERD tree
说明 :vim的插件NERDTree用于使得vim窗口分左右窗口显示的用法说明.其中,左侧为目录的树形界面,简称为NERDTree界面,右则为vim界面. 一.配置步骤 下载地址: http://ww ...
- load-store/register-memory/register-plus-memory比较
在理解ARM的load-store架构时,我在百度上搜索了很长时间,但是始终找不到一篇像样的中文文章.最后,在用谷歌搜索的英文网站上终于找到了一些蛛丝马迹.让我们先看一下一篇英文资料. Process ...
- 从 IT 的角度思考 BIM(一):面向对象
还记得那个笑话吗:要把大象放进冰箱,总共分几步?这不仅仅是一个笑话,还是一个值得我们好好分析的笑话. 如果要放进冰箱的是一个苹果,那么也就不可笑了,但换成大象,就引起了我们的兴趣和注意,为什么? 我们 ...
- 传感器- 加速计 - CoreMotion
/** * CoreMotion * */ #import "ViewController.h" #import <CoreMotion/CoreMotion.h> ...
- Tomcat基础教程(四)
一.将Web应用部署到Tomcat中 为什么要部署?将Web应用部署到Tomcat中,那么Tomcat就能找到相应的Web应用,当Tomcat启动时就会加载和初始化Web应用,而在Tomcat启动后, ...
- Computer Vision Applied to Super Resolution
Capel, David, and Andrew Zisserman. "Computer vision applied to super resolution." Signal ...