全部代碼如下:
ASPX:
<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="true" EnableViewState="false"
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 style="width: 100%; padding: 2px; font-weight: bold; background-color: #DEDEDE;
float: left">
<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" />
<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>
<input name="Date<%#Eval("id") %>" value="<%# ((DateTime)Eval("CreateDate")).ToString("yyyy-MM-dd") %>" style="border:1px solid gray"/>
</ItemTemplate>
</asp:TemplateField>
</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 = 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;