• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.net2.0中一次性更新所有GRIDVIEW的记录

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<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 >

  <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 >

  <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>


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Asp.net MVC2.0系列文章-添加操作发布时间:2022-07-10
下一篇:
DNX/ASP.NET5的xUnit入门向导发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap