ASP.NET(C#) GridView (编辑、删除、更新、取消)
2014-05-02 16:17
686 查看
转自:http://my.oschina.net/dldlhrmc/blog/93458
前台代码
viewsource
print?
后台代码
SQL脚本(SQLServer)
本人菜鸟,勿喷!仅供参考。
前台代码
01 | <%@PageLanguage= "C#" AutoEventWireup= "true" CodeFile= "Default5.aspx.cs" Inherits = "Default5" %> |
02 |
03 | <!DOCTYPEhtmlPUBLIC "-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > |
04 |
05 | <htmlxmlns= "http://www.w3.org/1999/xhtml" > |
06 | <headrunat= "server" > |
07 | <title></title> |
08 | </head> |
09 | <body> |
10 | <formid= "form1" runat= "server" > |
11 | <div> |
12 |
13 | <asp:GridViewID= "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" > |
18 | </asp:GridView> |
19 |
20 | </div> |
21 | </form> |
22 | </body> |
23 | </html> |
01 | usingSystem; |
02 | usingSystem.Collections.Generic; |
03 | usingSystem.Linq; |
04 | usingSystem.Web; |
05 | usingSystem.Web.UI; |
06 | usingSystem.Web.UI.WebControls; |
07 | usingSystem.Data.SqlClient; |
08 | usingSystem.Data; |
09 |
10 | publicpartialclassDefault5:System.Web.UI.Page |
11 | { |
12 | privatevoidBindData() |
13 | { |
14 | String Strcon= "DataSource=.;InitialCatalog=testDB;IntegratedSecurity=True" ; |
15 | SqlConnectioncon=newSqlConnection(Strcon); |
16 | String sql= "selectuserinfo.id,username,password,birthdayfromuserinfo" ; |
17 | SqlDataAdapterad=newSqlDataAdapter(sql,con); |
18 | DataSetds=newDataSet(); |
19 | ad.Fill(ds); |
20 | GridView1.DataSource=ds; |
21 | GridView1.DataBind(); |
22 | } |
23 |
24 | protectedvoidPage_Load(objectsender,EventArgse) |
25 | { |
26 | if(!IsPostBack)/*如果省略这句,下面的更新操作将无法完成,因为获得的值是不变的*/ |
27 | { |
28 | BindData(); |
29 | } |
30 | } |
31 | protectedvoidGridView1_RowDeleting(objectsender,GridViewDeleteEventArgse) |
32 | { |
33 | String Strcon= "DataSource=.;InitialCatalog=testDB;IntegratedSecurity=True" ; |
34 | SqlConnectioncon=newSqlConnection(Strcon); |
35 | intid=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);/*获取主键,需要设置DataKeyNames,这里设为id*/ |
36 | String sql= "deletefromuserinfowhereid='" +id+ "'" ; |
37 |
38 | SqlCommandcom=newSqlCommand(sql,con); |
39 | con.Open(); |
40 | com.ExecuteNonQuery(); |
41 | con.Close(); |
42 | BindData(); |
43 |
44 | } |
45 |
46 | /*编辑操作,利用e.NewEditIndex获取当前编辑行索引*/ |
47 | protectedvoidGridView1_RowEditing(objectsender,GridViewEditEventArgse) |
48 | { |
49 | GridView1.EditIndex=e.NewEditIndex; |
50 | BindData();/*再次绑定显示编辑行的原数据,不进行绑定要点2次编辑才能跳到编辑状态*/ |
51 | } |
52 | protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse) |
53 | { |
54 | String Strcon= "DataSource=.;InitialCatalog=testDB;IntegratedSecurity=True" ; |
55 | SqlConnectioncon=newSqlConnection(Strcon); |
56 | String username=(GridView1.Rows[e.RowIndex].Cells[2].Controls[0]asTextBox).Text.ToString();/*获取要更新的数据*/ |
57 | String password=(GridView1.Rows[e.RowIndex].Cells[3].Controls[0]asTextBox).Text.ToString(); |
58 | String birthday=(GridView1.Rows[e.RowIndex].Cells[4].Controls[0]asTextBox).Text.ToString(); |
59 |
60 | intid=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);/*获取主键,需要设置DataKeyNames,这里设为id*/ |
61 |
62 | String sql= "updateuserinfosetusername='" +username+ "',password='" +password+ "',birthday='" +birthday+ "'whereid='" +id+ "'" ; |
63 |
64 | SqlCommandcom=newSqlCommand(sql,con); |
65 | con.Open(); |
66 | com.ExecuteNonQuery(); |
67 | con.Close(); |
68 | GridView1.EditIndex=-1; |
69 | BindData(); |
70 | } |
71 | protectedvoidGridView1_RowCancelingEdit(objectsender,GridViewCancelEditEventArgse) |
72 | { |
73 | GridView1.EditIndex=-1;/*编辑索引赋值为-1,变回正常显示状态*/ |
74 | BindData(); |
75 | } |
76 | } |
01 | USE[testDB] |
02 | GO |
03 |
04 | /******Object: Table [dbo].[userinfo]Script Date :12/02/201222:20:46******/ |
05 | SET ANSI_NULLS ON |
06 | GO |
07 |
08 | SET QUOTED_IDENTIFIER ON |
09 | GO |
10 |
11 | SET ANSI_PADDING ON |
12 | GO |
13 |
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 |
20 | ( |
21 | [id] ASC |
22 | ) WITH (PAD_INDEX= OFF ,STATISTICS_NORECOMPUTE= OFF ,IGNORE_DUP_KEY= OFF ,ALLOW_ROW_LOCKS= ON ,ALLOW_PAGE_LOCKS= ON ) ON [ PRIMARY ] |
23 | ) ON [ PRIMARY ] |
24 |
25 | GO |
26 |
27 | SET ANSI_PADDING OFF |
28 | GO |
相关文章推荐
- asp.net c# 对GridView编辑,删除,更新,取消的操作
- [ASP.NET]GridView自定义编辑,更新,取消,删除
- asp.net中gridview的查询、分页、编辑更新、删除的实例代码
- asp.net中gridview的查询、分页、编辑更新、删除的实例代码
- Asp.Net[C#] 使用GridView 分页,编辑,删除,导出XML完整页面
- asp.net2.GridView选中,编辑,取消,删除
- Asp.Net[C#] 使用GridView 分页,编辑,删除,导出XML完整页面
- ASP.NET中运用DataList控件来实现编辑、更新、删除、取消等功能
- [Asp.Net]GridView无代码分页排序,选中,编辑,取消,删除,正反双向排序,下拉菜单DropDownList结合
- ASP.NET 中 对GridView(网格视图)的查、分页、编辑更新、删除操作
- Asp.Net[C#] 使用GridView 分页,编辑,删除,导出XML完整页面
- ASP.NET中Gridview读取数据表列表页面常用操作-序号,编辑,删除,删除确认
- GridView自定义模板编辑 更新 取消 删除
- 使用jQuery实现一个类似GridView的编辑,更新,取消和删除的功能
- GridView基本的操作 编辑 更新 取消 删除
- GridView行编辑、更新、取消、删除事件使用方法
- 使用gridview实现编辑、更新、取消功能和删除功能
- 关于ASP.NET中GridView控件中编辑和删除按钮布局问题
- ASP.NET中GridView动态绑定数据实现编辑更新
- asp.net之DataList的使用方法,及分页(存储过程创建),编辑,更新,删除