GridView, DataSource, RowUpdating, e.OldValues and e.NewValues collections
1
Regarding on the GridView control's RowUpdating event problem, it is the
2
expected behavior because when we do not associate GridView(or other
3
ASP.NET 2.0 databound control) with DataSource control, it won't
4
automatically query and fill the parameters collection of the
5
updating/deleting/
events. In such cases, we need to manually extract
6
the field values from the Template control. Fortunately , ASP.NET 2.0
7
provide some good helper functions which ease our work on extracting field
8
values from template databound control's datafields. e.g. the
9
BoundField.ExtractValuesFromCell method:
10
11
#BoundField.ExtractValuesFromCell Method
12
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfiel
13
d.extractvaluesfromcell.aspx
14
15
So in our GridView's RowUpdating event, we can manually query the data
16
fields values (key , old values , new values
) from the corresponding
17
cells. For example:
18
19
=======================
20
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs
21
e)
22
{
23
24
25
DataControlFieldCell cell = GridView1.Rows[e.RowIndex].Cells[0] as
26
DataControlFieldCell;
27
28
29
GridView1.Columns[0].ExtractValuesFromCell(
30
e.Keys,
31
cell,
32
DataControlRowState.Normal,
33
true);
34
35
36
cell = GridView1.Rows[e.RowIndex].Cells[1] as DataControlFieldCell;
37
38
39
GridView1.Columns[1].ExtractValuesFromCell(
40
e.NewValues,
41
cell,
42
DataControlRowState.Edit,
43
true);
44
45
46
foreach (string key in e.Keys.Keys)
47
{
48
response.write("<br/>" + key + ": " + e.Keys[key]);
49
}
50
51
foreach (string key in e.NewValues.Keys)
52
{
53
response.write("<br/>" + key + ": " + e.NewValues[key]);
54
}
55
链接地址:http://www.developersdex.com/asp/message.asp?p=2908&ID=%3CuvU%24WBARGHA.3052%40TK2MSFTNGP09.phx.gbl%3E
Regarding on the GridView control's RowUpdating event problem, it is the 2
expected behavior because when we do not associate GridView(or other 3
ASP.NET 2.0 databound control) with DataSource control, it won't 4
automatically query and fill the parameters collection of the 5
updating/deleting/
events. In such cases, we need to manually extract 6
the field values from the Template control. Fortunately , ASP.NET 2.0 7
provide some good helper functions which ease our work on extracting field 8
values from template databound control's datafields. e.g. the 9
BoundField.ExtractValuesFromCell method:10

11
#BoundField.ExtractValuesFromCell Method 12
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfiel13
d.extractvaluesfromcell.aspx14

15
So in our GridView's RowUpdating event, we can manually query the data 16
fields values (key , old values , new values
) from the corresponding 17
cells. For example:18

19
=======================20
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs 21
e)22
{23
24

25
DataControlFieldCell cell = GridView1.Rows[e.RowIndex].Cells[0] as 26
DataControlFieldCell;27
28

29
GridView1.Columns[0].ExtractValuesFromCell(30
e.Keys,31
cell,32
DataControlRowState.Normal,33
true);34

35

36
cell = GridView1.Rows[e.RowIndex].Cells[1] as DataControlFieldCell;37

38

39
GridView1.Columns[1].ExtractValuesFromCell(40
e.NewValues,41
cell,42
DataControlRowState.Edit,43
true);44

45

46
foreach (string key in e.Keys.Keys)47
{48
response.write("<br/>" + key + ": " + e.Keys[key]);49
}50

51
foreach (string key in e.NewValues.Keys)52
{53
response.write("<br/>" + key + ": " + e.NewValues[key]);54
}55


浙公网安备 33010602011771号