一次编辑 GridView 的所有行

本文介绍了一种在ASP.NET中高效批量编辑GridView控件中的多行数据的方法。通过使用SqlDataSource控件及其UpdateCommand属性,实现了对数据库的快速更新操作。此外,还详细展示了如何初始化控件状态以及处理提交数据的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在平时的实际项目中,为了减少往返提交数据,可以一次编辑 GridView 的所有行,这样大大提高用户使用产品的体验。下面就讲解何一次编辑 GridView 的所有数据行。在下面的例子中,数据库使用《ASP.NET 2.0应用开发技术》一书中Site/App_Data/下的 ASPNET20Book.mdb 数据库。例子中的数据更新方式使用 SqlDataSource 数据源控件的UpdateCommand属性,但此方法同样适用于存储过程,SQL 语句等。

C# 


DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
string[] ClassNameArray 
= { "大学""中学""高中" };

//初始化原来的值
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{    
  
if (e.Row.RowType == DataControlRowType.DataRow)
  {
    System.Data.DataRowView drv 
= (System.Data.DataRowView)e.Row.DataItem;
    RadioButtonList rbl 
= (RadioButtonList)e.Row.FindControl("txtGender");
    
if (rbl != null)
    {
      
if ((bool)drv["Gender"])
      {
        rbl.Items.FindByText(
"").Selected = true;
      }
      
else
      {
        rbl.Items.FindByText(
"").Selected = true;
      }
    }

    DropDownList ddl 
= (DropDownList)e.Row.FindControl("txtClassName");
    
if (ddl != null)
    {
      ddl.Items.FindByText(drv[
"ClassName"].ToString()).Selected = true;
    }
  }
}

// 初始化项目列表
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{    
  
if (e.Row.RowType == DataControlRowType.DataRow)
  {
    DropDownList ddl 
= (DropDownList)e.Row.FindControl("txtClassName");
    ddl.DataSource 
= ClassNameArray;
    ddl.DataBind();
  }
}

//处理提交的数据
protected void XianhuiMeng_Click(object sender, EventArgs e)
{
  foreach (GridViewRow gvr 
in GridView1.Rows)
  {
    string tGender 
= ((RadioButtonList)gvr.FindControl("txtGender")).SelectedValue;
    SqlDataSource1.UpdateParameters.Add(
new Parameter("@Title", TypeCode.String, ((TextBox)gvr.FindControl("txtTitle")).Text));
    SqlDataSource1.UpdateParameters.Add(
new Parameter("@Gender", TypeCode.Boolean, (tGender == "" ? "True" : "False")));
    SqlDataSource1.UpdateParameters.Add(
new Parameter("@ClassName"
                   TypeCode.String, ((DropDownList)gvr.FindControl(
"txtClassName")).SelectedValue));
    SqlDataSource1.UpdateParameters.Add(
new Parameter("@id", TypeCode.Int32, GridView1.DataKeys[gvr.DataItemIndex].Value.ToString()));
    SqlDataSource1.Update();
    SqlDataSource1.UpdateParameters.Clear();
  }
  Response.Redirect(Request.UrlReferrer.ToString());
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>同时编辑 GridView的 多行</title>
</head>
<body>
<form id="form1" runat="server">
  
<asp:GridView ID="GridView1" runat="server" Font-Size="12px" CellPadding="3" AutoGenerateColumns="False"
    DataKeyNames
="id" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound"
    OnRowCreated
="GridView1_RowCreated">
    
<HeaderStyle BackColor="#EDEDED" />
    
<Columns>
      
<asp:TemplateField HeaderText="姓名">
        
<ItemTemplate>
          
<asp:TextBox ID="txtTitle" runat="server" Text='
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值