asp.net 2.0中一次性更新所有GRIDVIEW的記錄

來源:互聯網
上載者:User
asp.net 在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>





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.