asp.net 2.0中一次性更新所有GRIDVIEW的记录

在asp.net 2.0中,gridview控件是十分不错的控件。有的时候,可能一个GRIDVIEW控件中
的各行都是文本框,如何一次性更新所有修改过的记录呢?有两种方法,一种是使用sqldatasource来更新
所有记录,但这个方法比较慢,因为每更新一条记录都要建立数据连接并执行updatecommand,会影响性能,
但还是先来看下实现方法:

1@ Page Language="C#" 
 1<script runat="server">
 2
 3void Button1_Click(object sender, EventArgs e) 
 4
 5{ 
 6
 7for (int i = 0; i < GridView1.Rows.Count; i++) 
 8
 9{ 
10
11GridViewRow row = GridView1.Rows[i]; 
12
13SqlDataSource1.UpdateParameters[0].DefaultValue = ((TextBox)row.Cells[0].FindControl("TextBox2")).Text; 
14
15SqlDataSource1.UpdateParameters[1].DefaultValue = ((TextBox)row.Cells[1].FindControl("TextBox3")).Text; 
16
17SqlDataSource1.UpdateParameters[2].DefaultValue = GridView1.DataKeys[i].Value.ToString(); 
18
19SqlDataSource1.Update(); 
20
21} 
22
23} 
24
25</script>
 1<html xmlns="http://www.w3.org/1999/xhtml">
 2<head runat="server">
 3<title>Untitled Page</title>
 4</head>
 5<body>
 6<form id="form1" runat="server">
 7<div>
 8<asp:gridview autogeneratecolumns="False" datakeynames="CustomerID" datasourceid="SqlDataSource1" id="GridView1" runat="server">
 9<columns>
10<asp:templatefield headertext="CustomerID" sortexpression="CustomerID">
11<itemtemplate>
12<asp:textbox id="TextBox1" runat="server" text='```
13# Bind("CustomerID") 
14```'></asp:textbox>
15</itemtemplate>
16</asp:templatefield>
17<asp:templatefield headertext="CompanyName" sortexpression="CompanyName">
18<itemtemplate>
19<asp:textbox id="TextBox2" runat="server" text='```
20# Bind("CompanyName") 
21```'></asp:textbox>
22</itemtemplate>
23</asp:templatefield>
24<asp:templatefield headertext="ContactTitle" sortexpression="ContactName">
25<itemtemplate>
26<asp:textbox id="TextBox3" runat="server" text='```
27# Bind("ContactTitle") 
28```'></asp:textbox>
29</itemtemplate>
30</asp:templatefield>
31</columns>
32</asp:gridview>
33<asp:sqldatasource connectionstring="```
34$ ConnectionStrings:AppConnectionString1 
35```" id="SqlDataSource1" runat="server" selectcommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]" updatecommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactTitle] = @ContactTitle WHERE [CustomerID] = @CustomerID">
36<updateparameters>
37<asp:parameter name="CompanyName" type="String"></asp:parameter>
38<asp:parameter name="ContactTitle" type="String"></asp:parameter>
39<asp:parameter name="CustomerID" type="String"></asp:parameter>
40</updateparameters>
41</asp:sqldatasource>
42<asp:button id="Button1" onclick="Button1_Click" runat="server" text="Button"></asp:button>  
43
44</div>
45</form>
46</body>
47</html>

另外一个方法是用组合SQL语句来进行的,速度比较快,原理也容易明白

1@ Page Language="C#" 
1@ Import Namespace="System.Text" 
1@ Import Namespace="System.Data.SqlClient" 
 1<script runat="server">
 2
 3void Button1_Click(object sender, EventArgs e) 
 4
 5{ 
 6
 7StringBuilder query = new StringBuilder(); 
 8
 9for (int i = 0; i < GridView1.Rows.Count; i++) 
10
11{ 
12
13GridViewRow row = GridView1.Rows[i]; 
14
15string value1 = ((TextBox)row.Cells[0].FindControl("TextBox2")).Text.Replace("'","''"); 
16
17string value2 = ((TextBox)row.Cells[1].FindControl("TextBox3")).Text.Replace("'","''"); 
18
19string value3 = GridView1.DataKeys[i].Value.ToString(); 
20
21query.Append("UPDATE [Customers] SET [CompanyName] = '") 
22
23.Append(value1).Append("' , [ContactTitle] = '") 
24
25.Append(value2).Append("' WHERE [CustomerID] = '") 
26
27.Append(value3).Append("';\n"); 
28
29} 
30
31SqlConnection con = new SqlConnection(ConfigurationSettings.ConnectionStrings["AppConnectionString1"].ConnectionString); 
32
33SqlCommand command = new SqlCommand(query.ToString(), con); 
34
35con.Open(); 
36
37command.ExecuteNonQuery(); 
38
39con.Close(); 
40
41} 
42
43void Page_Load(object sender, EventArgs e) 
44
45{ 
46
47if (!Page.IsPostBack) 
48
49{ 
50
51SqlConnection con = new SqlConnection(ConfigurationSettings.ConnectionStrings["AppConnectionString1"].ConnectionString); 
52
53SqlCommand command = new SqlCommand("SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]", con); 
54
55con.Open(); 
56
57GridView1.DataSource = command.ExecuteReader(); 
58
59GridView1.DataBind(); 
60
61con.Close(); 
62
63} 
64
65} 
66
67</script>
 1<html xmlns="http://www.w3.org/1999/xhtml">
 2<head runat="server">
 3<title>Untitled Page</title>
 4</head>
 5<body>
 6<form id="form1" runat="server">
 7<div>
 8<asp:gridview autogeneratecolumns="False" datakeynames="CustomerID" id="GridView1" runat="server">
 9<columns>
10<asp:templatefield headertext="CustomerID" sortexpression="CustomerID">
11<itemtemplate>
12<asp:textbox id="TextBox1" runat="server" text='```
13# Bind("CustomerID") 
14```'></asp:textbox>
15</itemtemplate>
16</asp:templatefield>
17<asp:templatefield headertext="CompanyName" sortexpression="CompanyName">
18<itemtemplate>
19<asp:textbox id="TextBox2" runat="server" text='```
20# Bind("CompanyName") 
21```'></asp:textbox>
22</itemtemplate>
23</asp:templatefield>
24<asp:templatefield headertext="ContactTitle" sortexpression="ContactName">
25<itemtemplate>
26<asp:textbox id="TextBox3" runat="server" text='```
27# Bind("ContactTitle") 
28```'></asp:textbox>
29</itemtemplate>
30</asp:templatefield>
31</columns>
32</asp:gridview>
33<asp:button id="Button1" onclick="Button1_Click" runat="server" text="Button"></asp:button>  
34
35</div>
36</form>
37</body>
38</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus