在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;
        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.Value.ToString();
        SqlDataSource1.Update();
      }
   }  
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form ></html>

相关文章:

  • 2021-08-14
  • 2022-12-23
  • 2021-11-05
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
  • 2021-08-08
猜你喜欢
  • 2021-06-20
  • 2022-12-23
  • 2021-09-27
  • 2022-02-16
相关资源
相似解决方案