前幾天寫了一個利用GridVIEw顯示主細表的例子,有網友詢問如何一次編輯明細表數據所有數據?其實,如果對ASP.Net或者Html比較熟悉的話,這種批量編輯數據的功能很容易實現。下面的例子使用隱藏表單域記錄數據的標識id,對每個輸入框的名字附加id信息,這樣的話,數據對應起來比較簡單。
例子中使用的數據庫來自《ASP.Net 2.0 應用開發技術》一書。換成其它數據庫也類似,看懂代碼,怎麼換就無妨了。
ASPx:
<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="true" EnableVIEwState="true"
CodeFile="Default2.ASPx.cs" Inherits="Default2" %>
<!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>
<style type="text/CSS">
td,div,a {font-size:12px}
</style>
<script type="text/Javascript">
//<![CDATA[
function ShowHidden(sid,ev)
{
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>
</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 >
<span >欄目名稱:<%#Eval("Title") %><;/span><span onclick="ShowHidden('<%#Eval("id") %>',event)">隱藏</span></div>
<div 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" />
<input name="Title<%#Eval("id") %>" value="<%#Eval("Title") %>" />
</ItemTemplate>
</ASP:TemplateFIEld>
<ASP:TemplateFIEld HeaderText="發布日期" HeaderStyle-Width="100px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<input name="Date<%#Eval("id") %>" value="<%# ((DateTime)Eval("CreateDate")).ToString("yyyy-MM-dd") %>" />
</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>
C#:
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;
public partial class Default2 : System.Web.UI.Page
{
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\ASPxWeb.mdb;Persist Security Info=True";
OleDbConnection cn1;
protected void Page_Load( object sender, EventArgs e )
{
if (!Page.IsPostBack)
{
OleDbConnection cn = newOleDbConnection(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 )
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView oGridView = (GridView)e.Row.FindControl("DetailGridVIEw");
if (oGridVIEw != null)
{
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();
}
}
}
protected void DetailGridView_RowDeleting( object sender, GridVIEwDeleteEventArgs e )
{
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 )
{
}
protected void Button1_Click( object sender, EventArgs e )
{
string Guids = Request.Form["guid"];
if (Guids == null || Guids.Equals(string.Empty))
{
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++)
{
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();
}
}