前几天写了一个利用GridView显示主细表的例子,有网友询问如何一次编辑明细表数据所有数据?其实,如果对ASP.NET或者HTML比较熟悉的话,这种批量编辑数据的功能很容易实现。下面的例子使用隐藏表单域记录数据的标识id,对每个输入框的名字附加id信息,这样的话,数据对应起来比较简单。
前几天写了一个利用GridView显示主细表的例子,有网友询问如何一次编辑明细表数据所有数据?其实,如果对ASP.NET或者HTML比较熟悉的话,这种批量编辑数据的功能很容易实现。下面的例子使用隐藏表单域记录数据的标识id,对每个输入框的名字附加id信息,这样的话,数据对应起来比较简单。
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Language="C#" ValidateRequest="false" AutoEventWireup="true" EnableViewState="true"
CodeFile="Default2.aspx.cs" Inherits="Default2" %>
![](/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>利用GridView显示主细表并一次编辑明细表所有数据的例子</title>
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<style type="text/css">![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
td,div,a {
}{font-size:12px}
</style>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script type="text/javascript">![](https://www.cnblogs.com/Images/dot.gif)
//<![CDATA[
function ShowHidden(sid,ev)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ev = ev || window.event;
var target = ev.target || ev.srcElement;
var oDiv = document.getElementById("div" + sid);
oDiv.style.display = oDiv.style.display == "none"?"block":"none";
target.innerHTML = oDiv.style.display == "none"?"显示":"隐藏";
}
//]]>
</script>
![](/Images/OutliningIndicators/None.gif)
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="MasterGridView" runat="server" AutoGenerateColumns="false" Width="780px"
BorderWidth="1" OnRowDataBound="MasterGridView_RowDataBound" DataKeyNames="id"
ShowHeader="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div style="width: 100%; padding: 2px; font-weight: bold; background-color: #DEDEDE;
float: left">
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<span style="float: left">栏目名称:<%
#Eval("Title") %></span><span style="float: right;
color: Red; cursor: pointer" onclick="ShowHidden('<%#Eval("id") %>',event)">隐藏</span></div>
<div style="background-color: #FFF; padding-left: 60px; clear: both" id="div<%#Eval("id") %>">
<asp:GridView ID="DetailGridView" runat="server" AutoGenerateColumns="false" ShowHeader="true"
HorizontalAlign="left" DataKeyNames="id" OnRowCommand="DetailGridView_RowCommand"
OnRowDeleting="DetailGridView_RowDeleting" Width="720px">
<HeaderStyle BackColor="#9999FF" />
<Columns>
<asp:TemplateField HeaderText="文章名称" HeaderStyle-Width="540px">
<ItemTemplate>
<input name="guid" value="<%#Eval("id") %>" type="hidden" />
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<input name="Title<%#Eval("id") %>" value="<%
#Eval("Title") %>" style="width: 100%;border:1px solid gray" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="发布日期" HeaderStyle-Width="100px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<input name="Date<%#Eval("id") %>" value="<%
# ((DateTime)Eval("CreateDate")).ToString("yyyy-MM-dd") %>" style="border:1px solid gray"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField DeleteText="删除" ShowDeleteButton="true" ButtonType="Button" HeaderStyle-Width="40px" />
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="编辑所有数据" />
</form>
</body>
</html>
![](/Images/OutliningIndicators/None.gif)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Web.UI.HtmlControls;
![](/Images/OutliningIndicators/None.gif)
public partial class Default2 : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aspxWeb.mdb;Persist Security Info=True";
OleDbConnection cn1;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
protected void Page_Load( object sender, EventArgs e )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Page.IsPostBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
OleDbConnection cn = new OleDbConnection(ConnectionString);
cn.Open();
cn1 = new OleDbConnection(ConnectionString);
cn1.Open();
OleDbCommand cmd = new OleDbCommand("select * from [Subject]", cn);
OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
MasterGridView.DataSource = dr;
MasterGridView.DataBind();
dr.Close();
cmd.Dispose();
cn.Dispose();
cn1.Dispose();
cn = cn1 = null;
}
}
protected void MasterGridView_RowDataBound( object sender, GridViewRowEventArgs e )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (e.Row.RowType == DataControlRowType.DataRow)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
GridView oGridView = (GridView)e.Row.FindControl("DetailGridView");
if (oGridView != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
OleDbCommand cmd = new OleDbCommand("select top 10 * from Document Where pid = " + MasterGridView.DataKeys[e.Row.RowIndex].Value, cn1);
OleDbDataReader dr1 = cmd.ExecuteReader();
oGridView.DataSource = dr1;
oGridView.DataBind();
dr1.Close();
cmd.Dispose();
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void DetailGridView_RowDeleting( object sender, GridViewDeleteEventArgs e )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GridView a = (GridView)sender;
Response.Write("您要删除的记录是:<font color='red'>" + a.DataKeys[e.RowIndex].Value.ToString() + "</font> TODO:执行删除动作");
// TODO:执行删除动作
}
protected void DetailGridView_RowCommand( object sender, GridViewCommandEventArgs e )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
}
protected void Button1_Click( object sender, EventArgs e )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string Guids = Request.Form["guid"];
if (Guids == null || Guids.Equals(string.Empty))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("没有数据可以修改。");
return;
}
string sql;
OleDbCommand cmd;
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = ConnectionString;
cn.Open();
string[] ArrGuid = Guids.Split(',');
for (int i = 0 ; i < ArrGuid.Length ; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sql = "UPDATE Document Set Title = @Title,CreateDate = @CreateDate Where id = @id";
cmd = new OleDbCommand(sql, cn);
cmd.Parameters.AddWithValue("@Title",Request.Form["Title" + ArrGuid[i].Trim()]);
cmd.Parameters.AddWithValue("@CreateDate", Request.Form["Date" + ArrGuid[i].Trim()]);
cmd.Parameters.AddWithValue("@id",ArrGuid[i].Trim());
cmd.ExecuteNonQuery();
Response.Write("<li>更新:id= " + ArrGuid[i] + " "+ Request.Form["Title" + ArrGuid[i].Trim()]);
}
cn.Close();
cn.Dispose();
}
}
![](/Images/OutliningIndicators/None.gif)