明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 321万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

Posted on   且行且思  阅读(339)  评论(0编辑  收藏  举报
 在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" />&nbsp;

   

    </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" />&nbsp;

   

    </div>

    </form>

</body>

</html>

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示