转自: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. < ...
随机推荐
- HTTP 417解决方案
在一次模拟HTPP请求时,本人在项目中的一般处理程序中调用客户接口返回非成功的结果.为了方便调试,所以将核心代码拷贝至控制台中进逐个调试. 在控制台中,启动调试时提示: 未经处理的异常: S ...
- ZOJ 1013 Great Equipment(DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=13 题目大意:说的是有三种不同的装备,分别是头盔,盔甲,战靴需要运输, ...
- linux管道学习(一)
最近学习了管道 pipe,在这里进行一下总结. 这里贴一段自己的实做代码 struct node{ int a; long b; }; int main() { ]; pid_t pid; ]; in ...
- Percona XtraBackup 备份原理
前言 Percona XtraBackup(简称PXB)是 Percona 公司开发的一个用于 MySQL 数据库物理热备的备份工具,支持 MySQl(Oracle).Percona Server 和 ...
- Fedora 21 安装QQ国际版
首先安装依赖包 sudo yum install freetype.i686 libpng.i686 libgcc.i686 libXau.i686 点击下载wine-2012qq国际版 unzip ...
- 网易邮箱前端Javascript编码规范:基础规范
在多年开发邮箱webmail过程中,网易邮箱前端团队积累了不少心得体会,我们开发了很多基础js库,实现了大量前端效果组件,开发了成熟的opoa框架以及api组件,在此向大家做一些分享.今天想先和大家聊 ...
- jbpm4.4 spring整合
jBPM-4.4与Spring集成配置比较容易,这里我使用的是Spring-2.5.6,数据库连接池使用C3P0,将相关的两个jar文件加入到CLASSPATH中. jBPM-4.4与Spring集成 ...
- Winform与WPF对话框(MessageBox, Dialog)之比较
Winform:使用System.Windows.Forms命名空间中相应控件; WPF则调用Microsoft.Win32. MessageBox: // WinForm private void ...
- 强大DevExpress,Winform LookUpEdit 实现多列查询 gridview弹出下拉选择 z
关键代码请参考http://www.devexpress.com/Support/Center/p/K18333.aspx 最新DEMO 下载 The current GridLookUpEdit's ...
- CCTV评论员评论步行者与奇才的比赛
步行者客场迎战主场作战的奇才,奇才的战士可能由于过度兴奋或是过度紧张身体僵硬,本来能打进的球都失掉了.反而,由于步行者取得了两位数的领先,越大心情越放松,打出了过去很少见的流畅局面. CCTV评论员就 ...