在ASP.Net 2.0中,gridview控件是十分不錯的控件。有的時候,可能一個GRIDVIEW控件中的各行都是文本框,如何一次性更新所有修改過的記錄呢?有兩種方法,一種是使用sqldatasource來更新所有記錄,但這個方法比較慢,因為每更新一條記錄都要建立數據連接並執行updatecommand,會影響性能,但還是先來看下實現方法:
<%@ Page Language="C#" %>
<script runat="server">
void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridVIEw1.Rows.Count; i++)
{
GridViewRow row = GridVIEw1.Rows[i];
SqlDataSource1.UpdateParameters[0].DefaultValue = ((TextBox)row.Cells[0].FindControl("TextBox2")).Text;
SqlDataSource1.UpdateParameters[1].DefaultValue = ((TextBox)row.Cells[1].FindControl("TextBox3")).Text;
SqlDataSource1.UpdateParameters[2].DefaultValue = GridVIEw1.DataKeys[i].Value.ToString();
SqlDataSource1.Update();
}
}
</script>
<html XMLns="http://www.w3.org/1999/xHtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ASP:GridView ID="GridVIEw1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID"
AutoGenerateColumns="False">
<Columns>
<ASP:TemplateFIEld SortExpression="CustomerID" HeaderText="CustomerID">
<ItemTemplate>
<ASP:TextBox Runat="server"
Text='<%# Bind("CustomerID") %>' ID="TextBox1"></ASP:TextBox>
</ItemTemplate>
</ASP:TemplateFIEld>
<ASP:TemplateFIEld SortExpression="CompanyName" HeaderText="CompanyName">
<ItemTemplate>
<asp:TextBox Runat="server" Text='<%# Bind("CompanyName") %>' ID="TextBox2"></ASP:TextBox>
</ItemTemplate>
</ASP:TemplateFIEld>
<ASP:TemplateFIEld SortExpression="ContactName" HeaderText="ContactTitle">
<ItemTemplate>
<asp:TextBox Runat="server" Text='<%# Bind("ContactTitle") %>' ID="TextBox3"></ASP:TextBox>
</ItemTemplate>
</ASP:TemplateFIEld>
</Columns>
</ASP:GridVIEw>
<ASP:SqlDataSource ID="SqlDataSource1" Runat="server"
SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]"
UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactTitle] = @ContactTitle WHERE [CustomerID] = @CustomerID"
ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>">
<UpdateParameters>
<asp:Parameter Type="String" Name="CompanyName"></ASP:Parameter>
<asp:Parameter Type="String" Name="ContactTitle"></ASP:Parameter>
<asp:Parameter Type="String" Name="CustomerID"></ASP:Parameter>
</UpdateParameters>
</ASP:SqlDataSource>
<ASP:Button ID="Button1" Runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</Html>
另外一個方法是用組合SQL語句來進行的,速度比較快,原理也容易明白
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Data.SqlClIEnt" %>
<script runat="server">
void Button1_Click(object sender, EventArgs e)
{
StringBuilder query = new StringBuilder();
for (int i = 0; i < GridVIEw1.Rows.Count; i++)
{
GridViewRow row = GridVIEw1.Rows[i];
string value1 = ((TextBox)row.Cells[0].FindControl("TextBox2")).Text.Replace("'","''");
string value2 = ((TextBox)row.Cells[1].FindControl("TextBox3")).Text.Replace("'","''");
string value3 = GridVIEw1.DataKeys[i].Value.ToString();
query.Append("UPDATE [Customers] SET [CompanyName] = '")
.Append(value1).Append("' , [ContactTitle] = '")
.Append(value2).Append("' WHERE [CustomerID] = '")
.Append(value3).Append("';\n");
}
SqlConnection con = new SqlConnection(ConfigurationSettings.ConnectionStrings["AppConnectionString1"].ConnectionString);
SqlCommand command = new SqlCommand(query.ToString(), con);
con.Open();
command.ExecuteNonQuery();
con.Close();
}
void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.ConnectionStrings["AppConnectionString1"].ConnectionString);
SqlCommand command = new SqlCommand("SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]", con);
con.Open();
GridVIEw1.DataSource = command.ExecuteReader();
GridVIEw1.DataBind();
con.Close();
}
}
</script>
<html XMLns="http://www.w3.org/1999/xHtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ASP:GridView ID="GridVIEw1" Runat="server" DataKeyNames="CustomerID"
AutoGenerateColumns="False">
<Columns>
<ASP:TemplateFIEld SortExpression="CustomerID" HeaderText="CustomerID">
<ItemTemplate>
<asp:TextBox Runat="server" Text='<%# Bind("CustomerID") %>' ID="TextBox1"></ASP:TextBox>
</ItemTemplate>
</ASP:TemplateFIEld>
<ASP:TemplateFIEld SortExpression="CompanyName" HeaderText="CompanyName">
<ItemTemplate>
<asp:TextBox Runat="server" Text='<%# Bind("CompanyName") %>' ID="TextBox2"></ASP:TextBox>
</ItemTemplate>
</ASP:TemplateFIEld>
<ASP:TemplateFIEld SortExpression="ContactName" HeaderText="ContactTitle">
<ItemTemplate>
<asp:TextBox Runat="server" Text='<%# Bind("ContactTitle") %>' ID="TextBox3"></ASP:TextBox>
</ItemTemplate>
</ASP:TemplateFIEld>
</Columns>
</ASP:GridVIEw>
<ASP:Button ID="Button1" Runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</Html>