您的位置:首页 > 编程语言 > ASP

ASP.NET(C#) GridView (编辑、删除、更新、取消)

2014-05-02 16:17 686 查看
转自:http://my.oschina.net/dldlhrmc/blog/93458

前台代码

viewsource

print?

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
}
SQL脚本(SQLServer)

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
本人菜鸟,勿喷!仅供参考。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: